-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add a check for some followed by filter #15745
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
Open
qmwrygdxpzc
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
ceptontech:filter-some
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−4
Open
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
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,61 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::pat_is_wild; | ||
use clippy_utils::res::{MaybeDef, MaybeResPath}; | ||
use clippy_utils::source::{indent_of, reindent_multiline, snippet_with_applicability, snippet_with_context}; | ||
use rustc_ast::util::parser::ExprPrecedence; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::LangItem::OptionSome; | ||
use rustc_hir::{Body, Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
|
||
use super::FILTER_SOME; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
recv: &'tcx Expr<'tcx>, | ||
arg: &'tcx Expr<'tcx>, | ||
msrv: Msrv, | ||
) { | ||
if !msrv.meets(cx, msrvs::BOOL_THEN_SOME) { | ||
return; | ||
} | ||
let mut applicability = Applicability::MaybeIncorrect; | ||
let value = match recv.kind { | ||
ExprKind::Call(f, [value]) if f.basic_res().ctor_parent(cx).is_lang_item(cx, OptionSome) => value, | ||
_ => return, | ||
}; | ||
let condition = if let ExprKind::Closure(c) = arg.kind | ||
&& let Body { | ||
params: [p], | ||
value: condition, | ||
} = cx.tcx.hir_body(c.body) | ||
&& pat_is_wild(cx, &p.pat.kind, arg) | ||
{ | ||
condition | ||
} else { | ||
return; | ||
}; | ||
let (condition_text, condition_is_macro) = | ||
snippet_with_context(cx, condition.span, arg.span.ctxt(), "..", &mut applicability); | ||
let parentheses = !condition_is_macro && cx.precedence(condition) < ExprPrecedence::Unambiguous; | ||
let value_text = snippet_with_applicability(cx, value.span, "..", &mut applicability); | ||
let sugg = format!( | ||
"{}{condition_text}{}.then_some({value_text})", | ||
if parentheses { "(" } else { "" }, | ||
if parentheses { ")" } else { "" }, | ||
); | ||
span_lint_and_then(cx, FILTER_SOME, expr.span, "use of `Some(_).filter(_)`", |diag| { | ||
diag.span_suggestion( | ||
expr.span, | ||
"consider using `then_some` instead", | ||
reindent_multiline(&sugg, true, indent_of(cx, expr.span)), | ||
applicability, | ||
); | ||
diag.note( | ||
"this change will alter the order in which the condition and the \ | ||
value are evaluated", | ||
); | ||
}); | ||
} |
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,40 @@ | ||
#![warn(clippy::filter_some)] | ||
|
||
macro_rules! unchanged { | ||
($result:expr) => { | ||
$result | ||
}; | ||
} | ||
|
||
macro_rules! condition { | ||
($condition:expr) => { | ||
$condition || false | ||
}; | ||
} | ||
|
||
fn main() { | ||
let _ = false.then_some(0); | ||
//~^ filter_some | ||
// The condition contains an operator. The program should add parentheses. | ||
let _ = (1 == 0).then_some(0); | ||
//~^ filter_some | ||
let _ = match 0 { | ||
//~^ filter_some | ||
0 => false, | ||
1 => true, | ||
_ => true, | ||
}.then_some(0); | ||
// The argument to filter requires the value in the option. The program | ||
// can't figure out how to change it. It should leave it alone for now. | ||
let _ = Some(0).filter(|x| *x == 0); | ||
// The expression is a macro argument. The program should change the macro | ||
// argument. It should not expand the macro. | ||
let _ = unchanged!(false.then_some(0)); | ||
//~^ filter_some | ||
let _ = vec![false].is_empty().then_some(0); | ||
//~^ filter_some | ||
// The condition is a macro that expands to an expression containing an | ||
// operator. The program should not add parentheses. | ||
let _ = condition!(false).then_some(0); | ||
//~^ filter_some | ||
} |
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,40 @@ | ||
#![warn(clippy::filter_some)] | ||
|
||
macro_rules! unchanged { | ||
($result:expr) => { | ||
$result | ||
}; | ||
} | ||
|
||
macro_rules! condition { | ||
($condition:expr) => { | ||
$condition || false | ||
}; | ||
} | ||
|
||
fn main() { | ||
let _ = Some(0).filter(|_| false); | ||
//~^ filter_some | ||
// The condition contains an operator. The program should add parentheses. | ||
let _ = Some(0).filter(|_| 1 == 0); | ||
//~^ filter_some | ||
let _ = Some(0).filter(|_| match 0 { | ||
//~^ filter_some | ||
0 => false, | ||
1 => true, | ||
_ => true, | ||
}); | ||
// The argument to filter requires the value in the option. The program | ||
// can't figure out how to change it. It should leave it alone for now. | ||
let _ = Some(0).filter(|x| *x == 0); | ||
// The expression is a macro argument. The program should change the macro | ||
// argument. It should not expand the macro. | ||
let _ = unchanged!(Some(0).filter(|_| false)); | ||
//~^ filter_some | ||
let _ = Some(0).filter(|_| vec![false].is_empty()); | ||
//~^ filter_some | ||
// The condition is a macro that expands to an expression containing an | ||
// operator. The program should not add parentheses. | ||
let _ = Some(0).filter(|_| condition!(false)); | ||
//~^ filter_some | ||
} |
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,67 @@ | ||
error: use of `Some(_).filter(_)` | ||
--> tests/ui/filter_some.rs:16:13 | ||
| | ||
LL | let _ = Some(0).filter(|_| false); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `then_some` instead: `false.then_some(0)` | ||
| | ||
= note: this change will alter the order in which the condition and the value are evaluated | ||
= note: `-D clippy::filter-some` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::filter_some)]` | ||
|
||
error: use of `Some(_).filter(_)` | ||
--> tests/ui/filter_some.rs:19:13 | ||
| | ||
LL | let _ = Some(0).filter(|_| 1 == 0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `then_some` instead: `(1 == 0).then_some(0)` | ||
| | ||
= note: this change will alter the order in which the condition and the value are evaluated | ||
|
||
error: use of `Some(_).filter(_)` | ||
--> tests/ui/filter_some.rs:21:13 | ||
| | ||
LL | let _ = Some(0).filter(|_| match 0 { | ||
| _____________^ | ||
LL | | | ||
LL | | 0 => false, | ||
LL | | 1 => true, | ||
LL | | _ => true, | ||
LL | | }); | ||
| |______^ | ||
| | ||
= note: this change will alter the order in which the condition and the value are evaluated | ||
help: consider using `then_some` instead | ||
| | ||
LL ~ let _ = match 0 { | ||
LL + | ||
LL + 0 => false, | ||
LL + 1 => true, | ||
LL + _ => true, | ||
LL ~ }.then_some(0); | ||
| | ||
|
||
error: use of `Some(_).filter(_)` | ||
--> tests/ui/filter_some.rs:32:24 | ||
| | ||
LL | let _ = unchanged!(Some(0).filter(|_| false)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `then_some` instead: `false.then_some(0)` | ||
| | ||
= note: this change will alter the order in which the condition and the value are evaluated | ||
|
||
error: use of `Some(_).filter(_)` | ||
--> tests/ui/filter_some.rs:34:13 | ||
| | ||
LL | let _ = Some(0).filter(|_| vec![false].is_empty()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `then_some` instead: `vec![false].is_empty().then_some(0)` | ||
| | ||
= note: this change will alter the order in which the condition and the value are evaluated | ||
|
||
error: use of `Some(_).filter(_)` | ||
--> tests/ui/filter_some.rs:38:13 | ||
| | ||
LL | let _ = Some(0).filter(|_| condition!(false)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `then_some` instead: `condition!(false).then_some(0)` | ||
| | ||
= note: this change will alter the order in which the condition and the value are evaluated | ||
|
||
error: aborting due to 6 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
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
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.
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.
Sugg::maybe_paren
has some logic to add parentheses, but only when needed -- I think that should be enough to deal with this case.(
Sugg::hir_with_context
is pretty much the same assnippet_with_context
)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.
With this change applied, the program adds parentheses here. Is it intended?
Uh oh!
There was an error while loading. Please reload this page.
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.
I assume that the previous version compiled just fine, and so the parentheses are indeed unnecessary. It could be that there are expressions for which your approach and
Sugg
would yield different results, and I must admit I don't which of those will actually have more correct ones, so I guess let's go with yours