Skip to content

Commit fec0057

Browse files
committed
Auto merge of #10013 - Jarcho:issue_9886, r=Manishearth
Don't lint `manual_assert` in `else if` fixes #9886 changelog: `manual_assert`: Don't lint in `else if`
2 parents e16a8b9 + 47fb67f commit fec0057

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::rustc_lint::LintContext;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::macros::{root_macro_call, FormatArgsExpn};
44
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::{peel_blocks_with_stmt, span_extract_comment, sugg};
5+
use clippy_utils::{is_else_clause, peel_blocks_with_stmt, span_extract_comment, sugg};
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, UnOp};
88
use rustc_lint::{LateContext, LateLintPass};
@@ -47,6 +47,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
4747
if cx.tcx.item_name(macro_call.def_id) == sym::panic;
4848
if !cx.tcx.sess.source_map().is_multiline(cond.span);
4949
if let Some(format_args) = FormatArgsExpn::find_nested(cx, then, macro_call.expn);
50+
// Don't change `else if foo { panic!(..) }` to `else { assert!(foo, ..) }` as it just
51+
// shuffles the condition around.
52+
// Should this have a config value?
53+
if !is_else_clause(cx.tcx, expr);
5054
then {
5155
let mut applicability = Applicability::MachineApplicable;
5256
let format_args_snip = snippet_with_applicability(cx, format_args.inputs_span(), "..", &mut applicability);

tests/ui/manual_assert.edition2018.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ fn main() {
6262
panic!("panic5");
6363
}
6464
assert!(!a.is_empty(), "with expansion {}", one!());
65+
if a.is_empty() {
66+
let _ = 0;
67+
} else if a.len() == 1 {
68+
panic!("panic6");
69+
}
6570
}
6671

6772
fn issue7730(a: u8) {

tests/ui/manual_assert.edition2021.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ fn main() {
5050
assert!(!(b.is_empty() || a.is_empty()), "panic4");
5151
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
5252
assert!(!a.is_empty(), "with expansion {}", one!());
53+
if a.is_empty() {
54+
let _ = 0;
55+
} else if a.len() == 1 {
56+
panic!("panic6");
57+
}
5358
}
5459

5560
fn issue7730(a: u8) {

tests/ui/manual_assert.edition2021.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ LL | | }
6565
| |_____^ help: try instead: `assert!(!a.is_empty(), "with expansion {}", one!());`
6666

6767
error: only a `panic!` in `if`-then statement
68-
--> $DIR/manual_assert.rs:73:5
68+
--> $DIR/manual_assert.rs:78:5
6969
|
7070
LL | / if a > 2 {
7171
LL | | // comment

tests/ui/manual_assert.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ fn main() {
6666
if a.is_empty() {
6767
panic!("with expansion {}", one!())
6868
}
69+
if a.is_empty() {
70+
let _ = 0;
71+
} else if a.len() == 1 {
72+
panic!("panic6");
73+
}
6974
}
7075

7176
fn issue7730(a: u8) {

0 commit comments

Comments
 (0)