Skip to content

Commit b7adf87

Browse files
authored
Merge pull request #2 from shreeed-app/dev
fix(missing_type): ignore macro-generated bodies in missing let type lint
2 parents 8d34340 + c1f6cf8 commit b7adf87

8 files changed

Lines changed: 114 additions & 106 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,44 @@ on:
55
branches: [master]
66
pull_request:
77

8+
concurrency:
9+
group: ci-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
permissions:
13+
contents: read
14+
15+
env:
16+
CARGO_TERM_COLOR: always
17+
818
jobs:
919
ci:
10-
name: Continuous Integration
20+
name: Dylint (nightly)
1121
runs-on: ubuntu-latest
1222

1323
steps:
14-
- uses: actions/checkout@v4
24+
- uses: actions/checkout@v6
1525

16-
- name: Install system dependencies
17-
run: |
18-
sudo apt-get update
19-
sudo apt-get install -y \
20-
pkg-config \
21-
libssl-dev \
22-
libgit2-dev \
23-
libcurl4-openssl-dev \
24-
build-essential
25-
26-
- uses: dtolnay/rust-toolchain@nightly
26+
- uses: dtolnay/rust-toolchain@v1
2727
with:
28-
components: rustc-dev,rust-src,llvm-tools-preview
28+
toolchain: nightly
29+
components: rustfmt, clippy, rustc-dev, rust-src, llvm-tools-preview
2930

30-
- uses: actions/cache@v4
31-
with:
32-
path: |
33-
~/.cargo/registry
34-
~/.cargo/git
35-
target
36-
~/.dylint_drivers
37-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
38-
restore-keys: |
39-
${{ runner.os }}-cargo-
40-
41-
- name: Install Dylint
42-
run: cargo install cargo-dylint dylint-link --locked
43-
44-
- name: Run Dylint
45-
run: cargo dylint --workspace --all
31+
- name: Ensure cargo in PATH
32+
run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH
33+
34+
- uses: Swatinem/rust-cache@v2
35+
36+
- uses: cargo-bins/cargo-binstall@main
37+
38+
- name: Install dependencies
39+
run: |
40+
cargo binstall -y \
41+
cargo-make \
42+
cargo-deny \
43+
cargo-audit \
44+
cargo-dylint \
45+
dylint-link
46+
47+
- name: Run CI
48+
run: cargo make ci

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# OS generated files
2+
.DS_Store
3+
Thumbs.db
4+
15
# Generated by Cargo
26
# will have compiled files and executables
37
debug

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dylint",
3131
"krate",
3232
"qpath",
33+
"rustflags",
3334
"rustsec",
3435
"rustup",
3536
"typeck",

Makefile.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,6 @@ args = [
115115
"ubuntu-latest=ghcr.io/catthehacker/ubuntu:full-latest",
116116
"--container-architecture",
117117
"linux/amd64",
118+
"--rm",
118119
]
119120
workspace = false

deny.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ confidence-threshold = 0.9
33
allow = [
44
"MIT",
55
"Apache-2.0",
6-
"BSD-2-Clause",
7-
"BSD-3-Clause",
8-
"ISC",
9-
"Zlib",
106
"Unicode-3.0",
11-
"CDLA-Permissive-2.0",
127
]
138

149
[bans]

rules/indexing_usage/src/lib.rs

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,47 @@ impl<'tcx> LateLintPass<'tcx> for SecurityIndexingUsage {
3333
context: &LateContext<'tcx>,
3434
expression: &'tcx Expr<'tcx>,
3535
) {
36-
match &expression.kind {
37-
ExprKind::Index(_, index_expr, _) => {
38-
match &index_expr.kind {
39-
// Literal indexing: array[0].
40-
ExprKind::Lit(_) => {
41-
context.span_lint(
42-
SECURITY_INDEXING_USAGE,
43-
expression.span,
44-
|diagnostic: &mut Diag<'_, ()>| {
45-
diagnostic.primary_message(
46-
"Usage of indexing operation detected.",
47-
);
48-
},
49-
);
50-
},
36+
if let ExprKind::Index(_, index_expr, _) = &expression.kind {
37+
match &index_expr.kind {
38+
// Literal indexing: array[0].
39+
ExprKind::Lit(_) => {
40+
context.span_lint(
41+
SECURITY_INDEXING_USAGE,
42+
expression.span,
43+
|diagnostic: &mut Diag<'_, ()>| {
44+
diagnostic.primary_message(
45+
"Usage of indexing operation detected.",
46+
);
47+
},
48+
);
49+
},
5150

52-
// Range slicing: array[1..], array[..], array[a..b].
53-
ExprKind::Struct(_, _, _) => {
54-
context.span_lint(
55-
SECURITY_INDEXING_USAGE,
56-
expression.span,
57-
|diagnostic: &mut Diag<'_, ()>| {
58-
diagnostic.primary_message(
59-
"Usage of slicing operation detected.",
60-
);
61-
},
62-
);
63-
},
51+
// Range slicing: array[1..], array[..], array[a..b].
52+
ExprKind::Struct(_, _, _) => {
53+
context.span_lint(
54+
SECURITY_INDEXING_USAGE,
55+
expression.span,
56+
|diagnostic: &mut Diag<'_, ()>| {
57+
diagnostic.primary_message(
58+
"Usage of slicing operation detected.",
59+
);
60+
},
61+
);
62+
},
6463

65-
// Any other dynamic indexing: array[i].
66-
_ => {
67-
context.span_lint(
68-
SECURITY_INDEXING_USAGE,
69-
expression.span,
70-
|diagnostic: &mut Diag<'_, ()>| {
71-
diagnostic.primary_message(
72-
"Usage of indexing operation detected.",
73-
);
74-
},
75-
);
76-
},
77-
}
78-
},
79-
80-
_ => {},
64+
// Any other dynamic indexing: array[i].
65+
_ => {
66+
context.span_lint(
67+
SECURITY_INDEXING_USAGE,
68+
expression.span,
69+
|diagnostic: &mut Diag<'_, ()>| {
70+
diagnostic.primary_message(
71+
"Usage of indexing operation detected.",
72+
);
73+
},
74+
);
75+
},
76+
}
8177
}
8278
}
8379

@@ -97,20 +93,18 @@ impl<'tcx> LateLintPass<'tcx> for SecurityIndexingUsage {
9793
if let ItemKind::Impl(implementation) = &item.kind
9894
&& let Some(trait_ref) = implementation.of_trait
9995
&& let Some(def_id) = trait_ref.trait_ref.path.res.opt_def_id()
96+
&& (context.tcx.lang_items().index_trait() == Some(def_id)
97+
|| context.tcx.lang_items().index_mut_trait() == Some(def_id))
10098
{
101-
if context.tcx.lang_items().index_trait() == Some(def_id)
102-
|| context.tcx.lang_items().index_mut_trait() == Some(def_id)
103-
{
104-
context.span_lint(
105-
SECURITY_INDEXING_USAGE,
106-
item.span,
107-
|diagnostic: &mut Diag<'_, ()>| {
108-
diagnostic.primary_message(
109-
"Implementation of Index/IndexMut trait detected.",
110-
);
111-
},
112-
);
113-
}
99+
context.span_lint(
100+
SECURITY_INDEXING_USAGE,
101+
item.span,
102+
|diagnostic: &mut Diag<'_, ()>| {
103+
diagnostic.primary_message(
104+
"Implementation of Index/IndexMut trait detected.",
105+
);
106+
},
107+
);
114108
}
115109
}
116110
}

rules/missing_type/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ extern crate rustc_hir;
55
extern crate rustc_lint;
66
extern crate rustc_middle;
77
extern crate rustc_session;
8+
extern crate rustc_span;
89

910
use rustc_errors::Diag;
10-
use rustc_hir::{Body, Expr, ExprKind, LetStmt, PatKind};
11+
use rustc_hir::{Body, BodyId, Expr, ExprKind, LetStmt, PatKind};
1112
use rustc_lint::{LateContext, LateLintPass, LintContext, LintStore};
1213
use rustc_middle::ty::TyCtxt;
1314
use rustc_session::{Session, declare_lint, declare_lint_pass};
@@ -55,6 +56,17 @@ impl<'tcx> LateLintPass<'tcx> for MissingType {
5556
if matches!(local.pat.kind, PatKind::Wild) {
5657
return;
5758
}
59+
60+
let Some(body_id): Option<BodyId> = context.enclosing_body else {
61+
return;
62+
};
63+
64+
// Skip if the let statement is from a macro expansion, as it may not
65+
// be possible to determine the type annotation in that case.
66+
if context.tcx.hir_body(body_id).value.span.from_expansion() {
67+
return;
68+
}
69+
5870
// Skip if the let statement is from a macro expansion, as it may not
5971
// be possible to determine the type annotation in that case.
6072
// Ignore anything coming from macro expansion (async_trait, derives,

rules/panic_usage/src/lib.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,20 @@ impl<'tcx> LateLintPass<'tcx> for SecurityPanicUsage {
7575
&& let Some(def_id) = context
7676
.typeck_results()
7777
.type_dependent_def_id(expression.hir_id)
78-
{
79-
if context.tcx.is_diagnostic_item(sym::unwrap, def_id)
78+
&& (context.tcx.is_diagnostic_item(sym::unwrap, def_id)
8079
|| context.tcx.is_diagnostic_item(sym::option_unwrap, def_id)
8180
|| context.tcx.is_diagnostic_item(sym::except, def_id)
82-
|| context.tcx.is_diagnostic_item(sym::option_expect, def_id)
83-
{
84-
context.span_lint(
85-
SECURITY_PANIC_USAGE,
86-
expression.span,
87-
|diagnostic: &mut Diag<'_, ()>| {
88-
diagnostic.primary_message(
89-
"Call to panic backend `unwrap/expect` detected.",
90-
);
91-
},
92-
);
93-
}
81+
|| context.tcx.is_diagnostic_item(sym::option_expect, def_id))
82+
{
83+
context.span_lint(
84+
SECURITY_PANIC_USAGE,
85+
expression.span,
86+
|diagnostic: &mut Diag<'_, ()>| {
87+
diagnostic.primary_message(
88+
"Call to panic backend `unwrap/expect` detected.",
89+
);
90+
},
91+
);
9492
}
9593

9694
// Detect calls to panic-related functions in the standard library.

0 commit comments

Comments
 (0)