-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint if_then_panic
#7669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New lint if_then_panic
#7669
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,97 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::higher::PanicExpn; | ||
use clippy_utils::is_expn_of; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Block, Expr, ExprKind, StmtKind, UnOp}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Detects `if`-then-`panic!` that can be replaced with `assert!`. | ||
/// | ||
/// ### Why is this bad? | ||
/// `assert!` is simpler than `if`-then-`panic!`. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let sad_people: Vec<&str> = vec![]; | ||
/// if !sad_people.is_empty() { | ||
/// panic!("there are sad people: {:?}", sad_people); | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let sad_people: Vec<&str> = vec![]; | ||
/// assert!(sad_people.is_empty(), "there are sad people: {:?}", sad_people); | ||
/// ``` | ||
pub IF_THEN_PANIC, | ||
style, | ||
"`panic!` and only a `panic!` in `if`-then statement" | ||
} | ||
|
||
declare_lint_pass!(IfThenPanic => [IF_THEN_PANIC]); | ||
|
||
impl LateLintPass<'_> for IfThenPanic { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if_chain! { | ||
if let Expr { | ||
kind: ExprKind:: If(cond, Expr { | ||
kind: ExprKind::Block( | ||
Block { | ||
stmts: [stmt], | ||
.. | ||
}, | ||
_), | ||
.. | ||
}, None), | ||
.. | ||
} = &expr; | ||
if is_expn_of(stmt.span, "panic").is_some(); | ||
if !matches!(cond.kind, ExprKind::Let(_, _, _)); | ||
if let StmtKind::Semi(semi) = stmt.kind; | ||
if !cx.tcx.sess.source_map().is_multiline(cond.span); | ||
|
||
then { | ||
let span = if let Some(panic_expn) = PanicExpn::parse(semi) { | ||
match *panic_expn.format_args.value_args { | ||
[] => panic_expn.format_args.format_string_span, | ||
[.., last] => panic_expn.format_args.format_string_span.to(last.span), | ||
} | ||
} else { | ||
if_chain! { | ||
if let ExprKind::Block(block, _) = semi.kind; | ||
if let Some(init) = block.expr; | ||
if let ExprKind::Call(_, [format_args]) = init.kind; | ||
|
||
then { | ||
format_args.span | ||
} else { | ||
return | ||
} | ||
} | ||
}; | ||
let mut applicability = Applicability::MachineApplicable; | ||
let sugg = snippet_with_applicability(cx, span, "..", &mut applicability); | ||
|
||
let cond_sugg = | ||
if let ExprKind::DropTemps(Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..}) = cond.kind { | ||
snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string() | ||
} else { | ||
format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability)) | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
IF_THEN_PANIC, | ||
expr.span, | ||
"only a `panic!` in `if`-then statement", | ||
"try", | ||
format!("assert!({}, {});", cond_sugg, sugg), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![deny(clippy::fallible_impl_from)] | ||
#![allow(clippy::if_then_panic)] | ||
|
||
// docs example | ||
struct Foo(i32); | ||
|
This file contains hidden or 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 hidden or 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,34 @@ | ||
// run-rustfix | ||
#![warn(clippy::if_then_panic)] | ||
|
||
fn main() { | ||
let a = vec![1, 2, 3]; | ||
let c = Some(2); | ||
if !a.is_empty() | ||
&& a.len() == 3 | ||
&& c != None | ||
&& !a.is_empty() | ||
&& a.len() == 3 | ||
&& !a.is_empty() | ||
&& a.len() == 3 | ||
&& !a.is_empty() | ||
&& a.len() == 3 | ||
{ | ||
panic!("qaqaq{:?}", a); | ||
} | ||
assert!(a.is_empty(), "qaqaq{:?}", a); | ||
assert!(a.is_empty(), "qwqwq"); | ||
if a.len() == 3 { | ||
println!("qwq"); | ||
println!("qwq"); | ||
println!("qwq"); | ||
} | ||
if let Some(b) = c { | ||
panic!("orz {}", b); | ||
} | ||
if a.len() == 3 { | ||
panic!("qaqaq"); | ||
} else { | ||
println!("qwq"); | ||
} | ||
} |
This file contains hidden or 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,38 @@ | ||
// run-rustfix | ||
#![warn(clippy::if_then_panic)] | ||
|
||
fn main() { | ||
let a = vec![1, 2, 3]; | ||
let c = Some(2); | ||
if !a.is_empty() | ||
&& a.len() == 3 | ||
&& c != None | ||
&& !a.is_empty() | ||
&& a.len() == 3 | ||
&& !a.is_empty() | ||
&& a.len() == 3 | ||
&& !a.is_empty() | ||
&& a.len() == 3 | ||
{ | ||
panic!("qaqaq{:?}", a); | ||
} | ||
if !a.is_empty() { | ||
panic!("qaqaq{:?}", a); | ||
} | ||
if !a.is_empty() { | ||
panic!("qwqwq"); | ||
} | ||
if a.len() == 3 { | ||
println!("qwq"); | ||
println!("qwq"); | ||
println!("qwq"); | ||
} | ||
if let Some(b) = c { | ||
panic!("orz {}", b); | ||
} | ||
if a.len() == 3 { | ||
panic!("qaqaq"); | ||
} else { | ||
println!("qwq"); | ||
} | ||
} |
This file contains hidden or 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,20 @@ | ||
error: only a `panic!` in `if`-then statement | ||
--> $DIR/if_then_panic.rs:19:5 | ||
| | ||
LL | / if !a.is_empty() { | ||
LL | | panic!("qaqaq{:?}", a); | ||
LL | | } | ||
| |_____^ help: try: `assert!(a.is_empty(), "qaqaq{:?}", a);` | ||
| | ||
= note: `-D clippy::if-then-panic` implied by `-D warnings` | ||
|
||
error: only a `panic!` in `if`-then statement | ||
--> $DIR/if_then_panic.rs:22:5 | ||
| | ||
LL | / if !a.is_empty() { | ||
LL | | panic!("qwqwq"); | ||
LL | | } | ||
| |_____^ help: try: `assert!(a.is_empty(), "qwqwq");` | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.