-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #7762 - HKalbasi:master, r=Manishearth
Add lint `equatable_if_let` This is my attempt for #1716. There is a major false positive, which is people may implement `PartialEq` in a different way. It is unactionable at the moment so I put it into `nursery`. There is a trait `StructuralPartialEq` for solving this problem which is promising but it has several problems currently: * Integers and tuples doesn't implement it. * Some types wrongly implement it, like `Option<T>` when `T` doesn't implement it. I consider them bugs and against the propose of `StructuralPartialEq`. When they become fixed, this lint can become a useful lint with a single line change. changelog: New lint: [`equatable_if_let`]
- Loading branch information
Showing
72 changed files
with
572 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::ty::implements_trait; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, Pat, PatKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::Ty; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for pattern matchings that can be expressed using equality. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// * It reads better and has less cognitive load because equality won't cause binding. | ||
/// * It is a [Yoda condition](https://en.wikipedia.org/wiki/Yoda_conditions). Yoda conditions are widely | ||
/// criticized for increasing the cognitive load of reading the code. | ||
/// * Equality is a simple bool expression and can be merged with `&&` and `||` and | ||
/// reuse if blocks | ||
/// | ||
/// ### Example | ||
/// ```rust,ignore | ||
/// if let Some(2) = x { | ||
/// do_thing(); | ||
/// } | ||
/// ``` | ||
/// Should be written | ||
/// ```rust,ignore | ||
/// if x == Some(2) { | ||
/// do_thing(); | ||
/// } | ||
/// ``` | ||
pub EQUATABLE_IF_LET, | ||
nursery, | ||
"using pattern matching instead of equality" | ||
} | ||
|
||
declare_lint_pass!(PatternEquality => [EQUATABLE_IF_LET]); | ||
|
||
/// detects if pattern matches just one thing | ||
fn unary_pattern(pat: &Pat<'_>) -> bool { | ||
fn array_rec(pats: &[Pat<'_>]) -> bool { | ||
pats.iter().all(unary_pattern) | ||
} | ||
match &pat.kind { | ||
PatKind::Slice(_, _, _) | PatKind::Range(_, _, _) | PatKind::Binding(..) | PatKind::Wild | PatKind::Or(_) => { | ||
false | ||
}, | ||
PatKind::Struct(_, a, etc) => !etc && a.iter().all(|x| unary_pattern(x.pat)), | ||
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => !etc.is_some() && array_rec(a), | ||
PatKind::Ref(x, _) | PatKind::Box(x) => unary_pattern(x), | ||
PatKind::Path(_) | PatKind::Lit(_) => true, | ||
} | ||
} | ||
|
||
fn is_structural_partial_eq(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: Ty<'tcx>) -> bool { | ||
if let Some(def_id) = cx.tcx.lang_items().eq_trait() { | ||
implements_trait(cx, ty, def_id, &[other.into()]) | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for PatternEquality { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
if_chain! { | ||
if let ExprKind::Let(pat, exp, _) = expr.kind; | ||
if unary_pattern(pat); | ||
let exp_ty = cx.typeck_results().expr_ty(exp); | ||
let pat_ty = cx.typeck_results().pat_ty(pat); | ||
if is_structural_partial_eq(cx, exp_ty, pat_ty); | ||
then { | ||
|
||
let mut applicability = Applicability::MachineApplicable; | ||
let pat_str = match pat.kind { | ||
PatKind::Struct(..) => format!( | ||
"({})", | ||
snippet_with_applicability(cx, pat.span, "..", &mut applicability), | ||
), | ||
_ => snippet_with_applicability(cx, pat.span, "..", &mut applicability).to_string(), | ||
}; | ||
span_lint_and_sugg( | ||
cx, | ||
EQUATABLE_IF_LET, | ||
expr.span, | ||
"this pattern matching can be expressed using equality", | ||
"try", | ||
format!( | ||
"{} == {}", | ||
snippet_with_applicability(cx, exp.span, "..", &mut applicability), | ||
pat_str, | ||
), | ||
applicability, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.