Skip to content

Commit b705107

Browse files
committed
Fix false-positive in equatable_if_let
1 parent 4198013 commit b705107

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

clippy_lints/src/equatable_if_let.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use clippy_utils::ty::implements_trait;
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, Pat, PatKind};
7-
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_lint::{LateContext, LateLintPass, LintContext};
8+
use rustc_middle::lint::in_external_macro;
89
use rustc_middle::ty::Ty;
910
use rustc_session::{declare_lint_pass, declare_tool_lint};
1011

@@ -67,6 +68,7 @@ fn is_structural_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: T
6768
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
6869
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
6970
if_chain! {
71+
if !in_external_macro(cx.sess(), expr.span);
7072
if let ExprKind::Let(let_expr) = expr.kind;
7173
if unary_pattern(let_expr.pat);
7274
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);

tests/ui/auxiliary/macro_rules.rs

+5
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,8 @@ macro_rules! manual_rem_euclid {
135135
let _: i32 = ((value % 4) + 4) % 4;
136136
};
137137
}
138+
139+
#[macro_export]
140+
macro_rules! equatable_if_let {
141+
($a:ident) => {{ if let 2 = $a {} }};
142+
}

tests/ui/equatable_if_let.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// run-rustfix
2+
// aux-build:macro_rules.rs
23

34
#![allow(unused_variables, dead_code, clippy::derive_partial_eq_without_eq)]
45
#![warn(clippy::equatable_if_let)]
56

7+
#[macro_use]
8+
extern crate macro_rules;
9+
610
use std::cmp::Ordering;
711

812
#[derive(PartialEq)]
@@ -75,4 +79,6 @@ fn main() {
7579
if "abc" == m1!(x) {
7680
println!("OK");
7781
}
82+
83+
equatable_if_let!(a);
7884
}

tests/ui/equatable_if_let.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// run-rustfix
2+
// aux-build:macro_rules.rs
23

34
#![allow(unused_variables, dead_code, clippy::derive_partial_eq_without_eq)]
45
#![warn(clippy::equatable_if_let)]
56

7+
#[macro_use]
8+
extern crate macro_rules;
9+
610
use std::cmp::Ordering;
711

812
#[derive(PartialEq)]
@@ -75,4 +79,6 @@ fn main() {
7579
if let m1!(x) = "abc" {
7680
println!("OK");
7781
}
82+
83+
equatable_if_let!(a);
7884
}

tests/ui/equatable_if_let.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
11
error: this pattern matching can be expressed using equality
2-
--> $DIR/equatable_if_let.rs:49:8
2+
--> $DIR/equatable_if_let.rs:53:8
33
|
44
LL | if let 2 = a {}
55
| ^^^^^^^^^ help: try: `a == 2`
66
|
77
= note: `-D clippy::equatable-if-let` implied by `-D warnings`
88

99
error: this pattern matching can be expressed using equality
10-
--> $DIR/equatable_if_let.rs:50:8
10+
--> $DIR/equatable_if_let.rs:54:8
1111
|
1212
LL | if let Ordering::Greater = a.cmp(&b) {}
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.cmp(&b) == Ordering::Greater`
1414

1515
error: this pattern matching can be expressed using equality
16-
--> $DIR/equatable_if_let.rs:51:8
16+
--> $DIR/equatable_if_let.rs:55:8
1717
|
1818
LL | if let Some(2) = c {}
1919
| ^^^^^^^^^^^^^^^ help: try: `c == Some(2)`
2020

2121
error: this pattern matching can be expressed using equality
22-
--> $DIR/equatable_if_let.rs:52:8
22+
--> $DIR/equatable_if_let.rs:56:8
2323
|
2424
LL | if let Struct { a: 2, b: false } = d {}
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `d == (Struct { a: 2, b: false })`
2626

2727
error: this pattern matching can be expressed using equality
28-
--> $DIR/equatable_if_let.rs:53:8
28+
--> $DIR/equatable_if_let.rs:57:8
2929
|
3030
LL | if let Enum::TupleVariant(32, 64) = e {}
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::TupleVariant(32, 64)`
3232

3333
error: this pattern matching can be expressed using equality
34-
--> $DIR/equatable_if_let.rs:54:8
34+
--> $DIR/equatable_if_let.rs:58:8
3535
|
3636
LL | if let Enum::RecordVariant { a: 64, b: 32 } = e {}
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == (Enum::RecordVariant { a: 64, b: 32 })`
3838

3939
error: this pattern matching can be expressed using equality
40-
--> $DIR/equatable_if_let.rs:55:8
40+
--> $DIR/equatable_if_let.rs:59:8
4141
|
4242
LL | if let Enum::UnitVariant = e {}
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::UnitVariant`
4444

4545
error: this pattern matching can be expressed using equality
46-
--> $DIR/equatable_if_let.rs:56:8
46+
--> $DIR/equatable_if_let.rs:60:8
4747
|
4848
LL | if let (Enum::UnitVariant, &Struct { a: 2, b: false }) = (e, &d) {}
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(e, &d) == (Enum::UnitVariant, &Struct { a: 2, b: false })`
5050

5151
error: this pattern matching can be expressed using equality
52-
--> $DIR/equatable_if_let.rs:66:8
52+
--> $DIR/equatable_if_let.rs:70:8
5353
|
5454
LL | if let NotStructuralEq::A = g {}
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `g == NotStructuralEq::A`
5656

5757
error: this pattern matching can be expressed using equality
58-
--> $DIR/equatable_if_let.rs:68:8
58+
--> $DIR/equatable_if_let.rs:72:8
5959
|
6060
LL | if let Some(NotStructuralEq::A) = Some(g) {}
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(g) == Some(NotStructuralEq::A)`
6262

6363
error: this pattern matching can be expressed using equality
64-
--> $DIR/equatable_if_let.rs:75:8
64+
--> $DIR/equatable_if_let.rs:79:8
6565
|
6666
LL | if let m1!(x) = "abc" {
6767
| ^^^^^^^^^^^^^^^^^^ help: try: `"abc" == m1!(x)`

0 commit comments

Comments
 (0)