Skip to content

Commit 958d9bb

Browse files
committed
Avoid to suggest pattern match on the similarly named in fn signature
1 parent 7950f24 commit 958d9bb

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

compiler/rustc_passes/src/liveness.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,10 @@ impl<'tcx> Liveness<'_, 'tcx> {
16911691
if ln == self.exit_ln { false } else { self.assigned_on_exit(ln, var) };
16921692

16931693
let mut typo = None;
1694-
for (hir_id, _, span) in &hir_ids_and_spans {
1694+
let filtered_hir_ids_and_spans = hir_ids_and_spans.iter().filter(|(hir_id, ..)| {
1695+
!matches!(self.ir.tcx.parent_hir_node(*hir_id), hir::Node::Param(_))
1696+
});
1697+
for (hir_id, _, span) in filtered_hir_ids_and_spans.clone() {
16951698
let ty = self.typeck_results.node_type(*hir_id);
16961699
if let ty::Adt(adt, _) = ty.peel_refs().kind() {
16971700
let name = Symbol::intern(&name);
@@ -1717,7 +1720,7 @@ impl<'tcx> Liveness<'_, 'tcx> {
17171720
}
17181721
}
17191722
if typo.is_none() {
1720-
for (hir_id, _, span) in &hir_ids_and_spans {
1723+
for (hir_id, _, span) in filtered_hir_ids_and_spans {
17211724
let ty = self.typeck_results.node_type(*hir_id);
17221725
// Look for consts of the same type with similar names as well, not just unit
17231726
// structs and variants.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/147303>.
2+
3+
#![deny(unused_assignments, unused_variables)]
4+
5+
mod m1 {
6+
const _MAX_FMTVER_X1X_EVENTNUM: i32 = 0;
7+
}
8+
9+
mod m2 {
10+
fn fun(rough: i32) {} //~ERROR unused variable
11+
}
12+
13+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unused variable: `rough`
2+
--> $DIR/invalid-sugg-for-unused-fn-arg-issue-147303.rs:11:12
3+
|
4+
LL | fn fun(rough: i32) {}
5+
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_rough`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/invalid-sugg-for-unused-fn-arg-issue-147303.rs:4:29
9+
|
10+
LL | #![deny(unused_assignments, unused_variables)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

tests/ui/lint/non-snake-case/lint-uppercase-variables.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,7 @@ warning: unused variable: `Foo`
5858
--> $DIR/lint-uppercase-variables.rs:33:17
5959
|
6060
LL | fn in_param(Foo: foo::Foo) {}
61-
| ^^^
62-
|
63-
help: if this is intentional, prefix it with an underscore
64-
|
65-
LL | fn in_param(_Foo: foo::Foo) {}
66-
| +
67-
help: you might have meant to pattern match on the similarly named variant `Foo`
68-
|
69-
LL | fn in_param(foo::Foo::Foo: foo::Foo) {}
70-
| ++++++++++
61+
| ^^^ help: if this is intentional, prefix it with an underscore: `_Foo`
7162

7263
error: structure field `X` should have a snake case name
7364
--> $DIR/lint-uppercase-variables.rs:10:5

0 commit comments

Comments
 (0)