Disable "if_not_else" lints firing on else-ifs
1. Convert IfNotElse to LateLintPass and use clippy_utils::is_else_clause for checking. 2. Handle the case where the span comes from desugaring. 3. Update tests.
This commit is contained in:
parent
00821ca937
commit
2f327aaba5
@ -2,9 +2,9 @@
|
||||
//! on the condition
|
||||
|
||||
use clippy_utils::diagnostics::span_lint_and_help;
|
||||
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, UnOp};
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use clippy_utils::is_else_clause;
|
||||
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
|
||||
declare_clippy_lint! {
|
||||
@ -46,14 +46,21 @@
|
||||
|
||||
declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
|
||||
|
||||
impl EarlyLintPass for IfNotElse {
|
||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
|
||||
if in_external_macro(cx.sess, item.span) {
|
||||
impl LateLintPass<'_> for IfNotElse {
|
||||
fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
|
||||
// While loops will be desugared to ExprKind::If. This will cause the lint to fire.
|
||||
// To fix this, return early if this span comes from a macro or desugaring.
|
||||
if item.span.from_expansion() {
|
||||
return;
|
||||
}
|
||||
if let ExprKind::If(ref cond, _, Some(ref els)) = item.kind {
|
||||
if let ExprKind::If(cond, _, Some(els)) = item.kind {
|
||||
if let ExprKind::Block(..) = els.kind {
|
||||
match cond.kind {
|
||||
// Disable firing the lint in "else if" expressions.
|
||||
if is_else_clause(cx.tcx, item) {
|
||||
return;
|
||||
}
|
||||
|
||||
match cond.peel_drop_temps().kind {
|
||||
ExprKind::Unary(UnOp::Not, _) => {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
|
@ -668,7 +668,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
store.register_early_pass(|| Box::new(double_parens::DoubleParens));
|
||||
store.register_late_pass(|| Box::new(to_string_in_display::ToStringInDisplay::new()));
|
||||
store.register_early_pass(|| Box::new(unsafe_removed_from_name::UnsafeNameRemoval));
|
||||
store.register_early_pass(|| Box::new(if_not_else::IfNotElse));
|
||||
store.register_early_pass(|| Box::new(else_if_without_else::ElseIfWithoutElse));
|
||||
store.register_early_pass(|| Box::new(int_plus_one::IntPlusOne));
|
||||
store.register_early_pass(|| Box::new(formatting::Formatting));
|
||||
@ -722,6 +721,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
store.register_late_pass(|| Box::new(option_if_let_else::OptionIfLetElse));
|
||||
store.register_late_pass(|| Box::new(future_not_send::FutureNotSend));
|
||||
store.register_late_pass(|| Box::new(if_let_mutex::IfLetMutex));
|
||||
store.register_late_pass(|| Box::new(if_not_else::IfNotElse));
|
||||
store.register_late_pass(|| Box::new(equatable_if_let::PatternEquality));
|
||||
store.register_late_pass(|| Box::new(mut_mutex_lock::MutMutexLock));
|
||||
store.register_late_pass(|| Box::new(match_on_vec_items::MatchOnVecItems));
|
||||
|
@ -1,6 +1,9 @@
|
||||
#![warn(clippy::all)]
|
||||
#![warn(clippy::if_not_else)]
|
||||
|
||||
fn foo() -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
fn bla() -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
@ -16,4 +19,11 @@ fn main() {
|
||||
} else {
|
||||
println!("Bunny");
|
||||
}
|
||||
if !foo() {
|
||||
println!("Foo");
|
||||
} else if !bla() {
|
||||
println!("Bugs");
|
||||
} else {
|
||||
println!("Bunny");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: unnecessary boolean `not` operation
|
||||
--> $DIR/if_not_else.rs:9:5
|
||||
--> $DIR/if_not_else.rs:12:5
|
||||
|
|
||||
LL | / if !bla() {
|
||||
LL | | println!("Bugs");
|
||||
@ -12,7 +12,7 @@ LL | | }
|
||||
= help: remove the `!` and swap the blocks of the `if`/`else`
|
||||
|
||||
error: unnecessary `!=` operation
|
||||
--> $DIR/if_not_else.rs:14:5
|
||||
--> $DIR/if_not_else.rs:17:5
|
||||
|
|
||||
LL | / if 4 != 5 {
|
||||
LL | | println!("Bugs");
|
||||
|
Loading…
Reference in New Issue
Block a user