Skip to content

Commit 4c70c18

Browse files
committed
Auto merge of rust-lang#7895 - ahmedkrmn:master, r=Manishearth
Disable "if_not_else" lints from firing on else-ifs Fixes rust-lang#7892 1. Convert `['if_not_else']` to `LateLintPass` and use `clippy_utils::is_else_clause` for checking. 2. Update tests. changelog: [`if_not_else`] now ignores else if statements.
2 parents dbe167d + 2f327aa commit 4c70c18

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

clippy_lints/src/if_not_else.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +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};
7-
use rustc_middle::lint::in_external_macro;
5+
use clippy_utils::is_else_clause;
6+
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
7+
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99

1010
declare_clippy_lint! {
@@ -46,14 +46,21 @@ declare_clippy_lint! {
4646

4747
declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
4848

49-
impl EarlyLintPass for IfNotElse {
50-
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
51-
if in_external_macro(cx.sess, item.span) {
49+
impl LateLintPass<'_> for IfNotElse {
50+
fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
51+
// While loops will be desugared to ExprKind::If. This will cause the lint to fire.
52+
// To fix this, return early if this span comes from a macro or desugaring.
53+
if item.span.from_expansion() {
5254
return;
5355
}
54-
if let ExprKind::If(ref cond, _, Some(ref els)) = item.kind {
56+
if let ExprKind::If(cond, _, Some(els)) = item.kind {
5557
if let ExprKind::Block(..) = els.kind {
56-
match cond.kind {
58+
// Disable firing the lint in "else if" expressions.
59+
if is_else_clause(cx.tcx, item) {
60+
return;
61+
}
62+
63+
match cond.peel_drop_temps().kind {
5764
ExprKind::Unary(UnOp::Not, _) => {
5865
span_lint_and_help(
5966
cx,

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));

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
}

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)