about summary refs log tree commit diff
path: root/compiler/rustc_passes/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2025-08-30 17:02:44 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-08-30 17:02:44 +0000
commit3355f4bced511aba46c5a92ea82241b6713331ba (patch)
tree6f3abebfe60198d6b71c9139a81994893bb03274 /compiler/rustc_passes/src
parent00f6fe1b6cbb211c4396b306566816f91a4ed371 (diff)
downloadrust-3355f4bced511aba46c5a92ea82241b6713331ba.tar.gz
rust-3355f4bced511aba46c5a92ea82241b6713331ba.zip
Suggest constant on unused binding in a pattern
```
error: unused variable: `Batery`
  --> $DIR/binding-typo-2.rs:110:9
   |
LL |         Batery => {}
   |         ^^^^^^
   |
help: if this is intentional, prefix it with an underscore
   |
LL |         _Batery => {}
   |         +
help: you might have meant to pattern match on the similarly named constant `Battery`
   |
LL |         Battery => {}
   |            +
```
Diffstat (limited to 'compiler/rustc_passes/src')
-rw-r--r--compiler/rustc_passes/src/liveness.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs
index 2ae68c445f8..b14482f65dc 100644
--- a/compiler/rustc_passes/src/liveness.rs
+++ b/compiler/rustc_passes/src/liveness.rs
@@ -1716,8 +1716,25 @@ impl<'tcx> Liveness<'_, 'tcx> {
                     }
                 }
             }
-            // FIXME(estebank): look for consts of the same type with similar names as well, not
-            // just unit structs and variants.
+            if typo.is_none() {
+                for (hir_id, _, span) in &hir_ids_and_spans {
+                    let ty = self.typeck_results.node_type(*hir_id);
+                    // Look for consts of the same type with similar names as well, not just unit
+                    // structs and variants.
+                    for def_id in self.ir.tcx.hir_body_owners() {
+                        if let DefKind::Const = self.ir.tcx.def_kind(def_id)
+                            && self.ir.tcx.type_of(def_id).instantiate_identity() == ty
+                        {
+                            typo = Some(errors::PatternTypo {
+                                span: *span,
+                                code: with_no_trimmed_paths!(self.ir.tcx.def_path_str(def_id)),
+                                kind: "constant".to_string(),
+                                item_name: self.ir.tcx.item_name(def_id).to_string(),
+                            });
+                        }
+                    }
+                }
+            }
             if is_assigned {
                 self.ir.tcx.emit_node_span_lint(
                     lint::builtin::UNUSED_VARIABLES,