Skip to content

Commit 282d313

Browse files
committed
Disable "if_not_else" lints firing on else-ifs
1. Convert IfNotElse to LateLintPass and use clippy_utils::is_else_clause for checking. 2. Update tests.
1 parent 00821ca commit 282d313

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

Diff for: clippy_lints/src/if_not_else.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
//! on the condition
33
44
use clippy_utils::diagnostics::span_lint_and_help;
5-
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, UnOp};
6-
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use clippy_utils::is_else_clause;
6+
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
7+
use rustc_lint::{LateContext, LateLintPass, LintContext};
78
use rustc_middle::lint::in_external_macro;
89
use rustc_session::{declare_lint_pass, declare_tool_lint};
910

@@ -46,13 +47,21 @@ declare_clippy_lint! {
4647

4748
declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
4849

49-
impl EarlyLintPass for IfNotElse {
50-
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
51-
if in_external_macro(cx.sess, item.span) {
50+
impl LateLintPass<'_> for IfNotElse {
51+
fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
52+
if in_external_macro(cx.sess(), item.span) {
5253
return;
5354
}
54-
if let ExprKind::If(ref cond, _, Some(ref els)) = item.kind {
55+
if let ExprKind::If(mut cond, _, Some(els)) = item.kind {
5556
if let ExprKind::Block(..) = els.kind {
57+
// Disable firing the lint in "else if" expressions.
58+
if is_else_clause(cx.tcx, item) {
59+
return;
60+
}
61+
62+
if let ExprKind::DropTemps(inner) = &cond.kind {
63+
cond = inner;
64+
}
5665
match cond.kind {
5766
ExprKind::Unary(UnOp::Not, _) => {
5867
span_lint_and_help(

Diff for: clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
668668
store.register_early_pass(|| Box::new(double_parens::DoubleParens));
669669
store.register_late_pass(|| Box::new(to_string_in_display::ToStringInDisplay::new()));
670670
store.register_early_pass(|| Box::new(unsafe_removed_from_name::UnsafeNameRemoval));
671-
store.register_early_pass(|| Box::new(if_not_else::IfNotElse));
672671
store.register_early_pass(|| Box::new(else_if_without_else::ElseIfWithoutElse));
673672
store.register_early_pass(|| Box::new(int_plus_one::IntPlusOne));
674673
store.register_early_pass(|| Box::new(formatting::Formatting));
@@ -722,6 +721,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
722721
store.register_late_pass(|| Box::new(option_if_let_else::OptionIfLetElse));
723722
store.register_late_pass(|| Box::new(future_not_send::FutureNotSend));
724723
store.register_late_pass(|| Box::new(if_let_mutex::IfLetMutex));
724+
store.register_late_pass(|| Box::new(if_not_else::IfNotElse));
725725
store.register_late_pass(|| Box::new(equatable_if_let::PatternEquality));
726726
store.register_late_pass(|| Box::new(mut_mutex_lock::MutMutexLock));
727727
store.register_late_pass(|| Box::new(match_on_vec_items::MatchOnVecItems));

Diff for: tests/ui/if_not_else.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![warn(clippy::all)]
22
#![warn(clippy::if_not_else)]
33

4+
fn foo() -> bool {
5+
unimplemented!()
6+
}
47
fn bla() -> bool {
58
unimplemented!()
69
}
@@ -16,4 +19,11 @@ fn main() {
1619
} else {
1720
println!("Bunny");
1821
}
22+
if !foo() {
23+
println!("Foo");
24+
} else if !bla() {
25+
println!("Bugs");
26+
} else {
27+
println!("Bunny");
28+
}
1929
}

Diff for: tests/ui/if_not_else.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnecessary boolean `not` operation
2-
--> $DIR/if_not_else.rs:9:5
2+
--> $DIR/if_not_else.rs:12:5
33
|
44
LL | / if !bla() {
55
LL | | println!("Bugs");
@@ -12,7 +12,7 @@ LL | | }
1212
= help: remove the `!` and swap the blocks of the `if`/`else`
1313

1414
error: unnecessary `!=` operation
15-
--> $DIR/if_not_else.rs:14:5
15+
--> $DIR/if_not_else.rs:17:5
1616
|
1717
LL | / if 4 != 5 {
1818
LL | | println!("Bugs");

0 commit comments

Comments
 (0)