Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5b710ee

Browse files
committed
Auto merge of rust-lang#4977 - krishna-veerareddy:issue-4969-replace-consts-fp, r=phansch
Prevent `replace_consts` lint within match patterns Currently `replace_consts` lint applies within match patterns but the suggestion is incorrect as function calls are disallowed in them. To fix this we prevent the lint from firing within patterns. Fixes rust-lang#4969 changelog: Fix false positive in `replace_consts` lint
2 parents 99dd0bb + 8b36196 commit 5b710ee

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

clippy_lints/src/replace_consts.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::utils::{match_def_path, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc::declare_lint_pass;
4-
use rustc::hir;
54
use rustc::hir::def::{DefKind, Res};
5+
use rustc::hir::*;
66
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
77
use rustc_errors::Applicability;
88
use rustc_session::declare_tool_lint;
@@ -34,11 +34,26 @@ declare_clippy_lint! {
3434

3535
declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
3636

37+
fn in_pattern(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
38+
let map = &cx.tcx.hir();
39+
let parent_id = map.get_parent_node(expr.hir_id);
40+
41+
if let Some(node) = map.find(parent_id) {
42+
if let Node::Pat(_) = node {
43+
return true;
44+
}
45+
}
46+
47+
false
48+
}
49+
3750
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
38-
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
51+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
3952
if_chain! {
40-
if let hir::ExprKind::Path(ref qp) = expr.kind;
53+
if let ExprKind::Path(ref qp) = expr.kind;
4154
if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
55+
// Do not lint within patterns as function calls are disallowed in them
56+
if !in_pattern(cx, expr);
4257
then {
4358
for &(ref const_path, repl_snip) in &REPLACEMENTS {
4459
if match_def_path(cx, def_id, const_path) {

tests/ui/replace_consts.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ fn good() {
7575
{ let foo = u32::max_value(); };
7676
{ let foo = u64::max_value(); };
7777
{ let foo = u128::max_value(); };
78+
79+
let x = 42;
80+
81+
let _ = match x {
82+
std::i8::MIN => -1,
83+
1..=std::i8::MAX => 1,
84+
_ => 0
85+
};
86+
87+
let _ = if let std::i8::MIN = x {
88+
-1
89+
} else if let 1..=std::i8::MAX = x {
90+
1
91+
} else {
92+
0
93+
};
7894
}
7995

8096
fn main() {

tests/ui/replace_consts.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ fn good() {
7575
{ let foo = u32::max_value(); };
7676
{ let foo = u64::max_value(); };
7777
{ let foo = u128::max_value(); };
78+
79+
let x = 42;
80+
81+
let _ = match x {
82+
std::i8::MIN => -1,
83+
1..=std::i8::MAX => 1,
84+
_ => 0
85+
};
86+
87+
let _ = if let std::i8::MIN = x {
88+
-1
89+
} else if let 1..=std::i8::MAX = x {
90+
1
91+
} else {
92+
0
93+
};
7894
}
7995

8096
fn main() {

0 commit comments

Comments
 (0)