Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent replace_consts lint within match patterns #4977

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions clippy_lints/src/replace_consts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::utils::{match_def_path, span_lint_and_sugg};
use if_chain::if_chain;
use rustc::declare_lint_pass;
use rustc::hir;
use rustc::hir::def::{DefKind, Res};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc_errors::Applicability;
use rustc_session::declare_tool_lint;
Expand Down Expand Up @@ -34,11 +34,26 @@ declare_clippy_lint! {

declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);

fn in_pattern(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
let map = &cx.tcx.hir();
let parent_id = map.get_parent_node(expr.hir_id);

if let Some(node) = map.find(parent_id) {
if let Node::Pat(_) = node {
return true;
}
}

false
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
if_chain! {
if let hir::ExprKind::Path(ref qp) = expr.kind;
if let ExprKind::Path(ref qp) = expr.kind;
if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
// Do not lint within patterns as function calls are disallowed in them
if !in_pattern(cx, expr);
then {
for &(ref const_path, repl_snip) in &REPLACEMENTS {
if match_def_path(cx, def_id, const_path) {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/replace_consts.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ fn good() {
{ let foo = u32::max_value(); };
{ let foo = u64::max_value(); };
{ let foo = u128::max_value(); };

let _ = match 42 {
std::i8::MIN => -1,
1..=std::i8::MAX => 1,
_ => 0
};
}

fn main() {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/replace_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ fn good() {
{ let foo = u32::max_value(); };
{ let foo = u64::max_value(); };
{ let foo = u128::max_value(); };

let _ = match 42 {
std::i8::MIN => -1,
1..=std::i8::MAX => 1,
_ => 0
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test with if let as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I didn't think about that use case. Done.

}

fn main() {
Expand Down