-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
auto-fix if_not_else
#13809
base: master
Are you sure you want to change the base?
auto-fix if_not_else
#13809
Conversation
r? @Alexendoo rustbot has assigned @Alexendoo. Use |
b9f5379
to
ab5cde9
Compare
clippy_lints/src/if_not_else.rs
Outdated
@@ -54,7 +58,7 @@ fn is_zero_const(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool { | |||
|
|||
impl LateLintPass<'_> for IfNotElse { | |||
fn check_expr(&mut self, cx: &LateContext<'_>, e: &Expr<'_>) { | |||
if let ExprKind::If(cond, _, Some(els)) = e.kind | |||
if let ExprKind::If(cond, inter, Some(els)) = e.kind |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The processing performed by the if
statement is necessary to present the proposed modification in the make_sugg
function.
0322f5f
to
654c288
Compare
654c288
to
1ace535
Compare
r? clippy |
I'd like to see more test cases involving comments and annotations, just to be sure the suggestion reproduces them faithfully. |
if !(foo() && bla()) { | ||
#[cfg(not(debug_assertions))] | ||
println!("not debug"); | ||
#[cfg(debug_assertions)] | ||
println!("debug"); | ||
if foo() { | ||
println!("foo"); | ||
} else if bla() { | ||
println!("bla"); | ||
} else { | ||
println!("both false"); | ||
} | ||
} else { | ||
println!("both true"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering the following code, which is similar in structure to this one, except that the parts to be linted are nested:
if !(foo() && bla()) {
if !foo() {
println!("not foo");
} else {
println!("some output");
}
} else {
println!("both true");
}
Then, when I run cargo bless
, I get a cannot replace slice of data that was already replaced
error. But I don't think this is a problem since rustfix
will apply the first fix and ignore the subsequent when put to practical use.
fix #13411
The
if_not_else
lint can be fixed automatically, but the issue above reports that there is no implementation to do so. Therefore, this PR implements it.changelog: [
if_not_else
]: make suggestions for modified code