Fix false-positive in equatable_if_let

This commit is contained in:
dAxpeDDa 2022-06-30 17:45:34 +02:00
parent 4198013522
commit b7051077c9
No known key found for this signature in database
GPG Key ID: 43D62A3EA388E46F
5 changed files with 31 additions and 12 deletions

View File

@ -4,7 +4,8 @@
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Pat, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::Ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -67,6 +68,7 @@ fn is_structural_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: T
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if_chain! {
if !in_external_macro(cx.sess(), expr.span);
if let ExprKind::Let(let_expr) = expr.kind;
if unary_pattern(let_expr.pat);
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);

View File

@ -135,3 +135,8 @@ macro_rules! manual_rem_euclid {
let _: i32 = ((value % 4) + 4) % 4;
};
}
#[macro_export]
macro_rules! equatable_if_let {
($a:ident) => {{ if let 2 = $a {} }};
}

View File

@ -1,8 +1,12 @@
// run-rustfix
// aux-build:macro_rules.rs
#![allow(unused_variables, dead_code, clippy::derive_partial_eq_without_eq)]
#![warn(clippy::equatable_if_let)]
#[macro_use]
extern crate macro_rules;
use std::cmp::Ordering;
#[derive(PartialEq)]
@ -75,4 +79,6 @@ fn main() {
if "abc" == m1!(x) {
println!("OK");
}
equatable_if_let!(a);
}

View File

@ -1,8 +1,12 @@
// run-rustfix
// aux-build:macro_rules.rs
#![allow(unused_variables, dead_code, clippy::derive_partial_eq_without_eq)]
#![warn(clippy::equatable_if_let)]
#[macro_use]
extern crate macro_rules;
use std::cmp::Ordering;
#[derive(PartialEq)]
@ -75,4 +79,6 @@ macro_rules! m1 {
if let m1!(x) = "abc" {
println!("OK");
}
equatable_if_let!(a);
}

View File

@ -1,5 +1,5 @@
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:49:8
--> $DIR/equatable_if_let.rs:53:8
|
LL | if let 2 = a {}
| ^^^^^^^^^ help: try: `a == 2`
@ -7,61 +7,61 @@ LL | if let 2 = a {}
= note: `-D clippy::equatable-if-let` implied by `-D warnings`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:50:8
--> $DIR/equatable_if_let.rs:54:8
|
LL | if let Ordering::Greater = a.cmp(&b) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.cmp(&b) == Ordering::Greater`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:51:8
--> $DIR/equatable_if_let.rs:55:8
|
LL | if let Some(2) = c {}
| ^^^^^^^^^^^^^^^ help: try: `c == Some(2)`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:52:8
--> $DIR/equatable_if_let.rs:56:8
|
LL | if let Struct { a: 2, b: false } = d {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `d == (Struct { a: 2, b: false })`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:53:8
--> $DIR/equatable_if_let.rs:57:8
|
LL | if let Enum::TupleVariant(32, 64) = e {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::TupleVariant(32, 64)`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:54:8
--> $DIR/equatable_if_let.rs:58:8
|
LL | if let Enum::RecordVariant { a: 64, b: 32 } = e {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == (Enum::RecordVariant { a: 64, b: 32 })`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:55:8
--> $DIR/equatable_if_let.rs:59:8
|
LL | if let Enum::UnitVariant = e {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::UnitVariant`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:56:8
--> $DIR/equatable_if_let.rs:60:8
|
LL | if let (Enum::UnitVariant, &Struct { a: 2, b: false }) = (e, &d) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(e, &d) == (Enum::UnitVariant, &Struct { a: 2, b: false })`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:66:8
--> $DIR/equatable_if_let.rs:70:8
|
LL | if let NotStructuralEq::A = g {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `g == NotStructuralEq::A`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:68:8
--> $DIR/equatable_if_let.rs:72:8
|
LL | if let Some(NotStructuralEq::A) = Some(g) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(g) == Some(NotStructuralEq::A)`
error: this pattern matching can be expressed using equality
--> $DIR/equatable_if_let.rs:75:8
--> $DIR/equatable_if_let.rs:79:8
|
LL | if let m1!(x) = "abc" {
| ^^^^^^^^^^^^^^^^^^ help: try: `"abc" == m1!(x)`