-
Notifications
You must be signed in to change notification settings - Fork 1.8k
new lint: [or_else_then_unwrap
]
#15734
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
base: master
Are you sure you want to change the base?
Conversation
8307906
to
8f7cade
Compare
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.
Looking pretty good:) Left some minor comments
} | ||
|
||
fn get_content_if_ctor_matches_in_closure(cx: &LateContext<'_>, expr: &Expr<'_>, item: LangItem) -> Option<Span> { | ||
if let ExprKind::Closure(closure) = expr.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.
No idea how likely this is, but you could add a check for the Ok
/Some
coming from a macro expansion -- e.g., the user can't really do anything about a case like:
fn main() {
macro_rules! some { () => { Some(5) } };
None.or_else(|| some!()).unwrap();
See this section for more info: https://doc.rust-lang.org/clippy/development/macro_expansions.html#spanctxt-method
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 put in a test case for this with proc_macros::external!{
and it doesn't appear to be triggering the lint without adding in_external_macro
check... I'd like to understand why that is before adding the code to intentionally avoid - any idea why this would be getting ignored already? Is that test the right approach, or is there e.g. something special to know about proc_macros::external
?
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.
Ah, right, that's because methods/mod.rs
mostly filters out any method calls coming from an expansion:
rust-clippy/clippy_lints/src/methods/mod.rs
Lines 4970 to 4972 in 95dd88d
fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | |
// Handle method calls whose receiver and arguments may not come from expansion | |
if let Some((name, recv, args, span, call_span)) = method_call(expr) { |
rust-clippy/clippy_lints/src/methods/mod.rs
Lines 4852 to 4864 in 95dd88d
/// Extracts a method call name, args, and `Span` of the method name. | |
/// This ensures that neither the receiver nor any of the arguments | |
/// come from expansion. | |
pub fn method_call<'tcx>(recv: &'tcx Expr<'tcx>) -> Option<(Symbol, &'tcx Expr<'tcx>, &'tcx [Expr<'tcx>], Span, Span)> { | |
if let ExprKind::MethodCall(path, receiver, args, call_span) = recv.kind | |
&& !args.iter().any(|e| e.span.from_expansion()) | |
&& !receiver.span.from_expansion() | |
{ | |
Some((path.ident.name, receiver, args, path.ident.span, call_span)) | |
} else { | |
None | |
} | |
} |
This might change in the future, but for now, the check I was talking about is indeed unnecessary.
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
This is the same as or_then_unwrap but for or_else calls with a closure immediately calling `Some`.
Co-authored-by: Ada Alakbarova <[email protected]>
Co-authored-by: Ada Alakbarova <[email protected]>
Co-authored-by: Ada Alakbarova <[email protected]>
Co-authored-by: Ada Alakbarova <[email protected]>
Co-authored-by: Ada Alakbarova <[email protected]>
e5d9c71
to
0f37275
Compare
Thanks so much for the suggestions @ada4a, I really appreciate them! I do want to call out - I pretty much copied this directly from the |
Glad to have helped @illicitonion :) I took a look at |
changelog: [
or_else_then_unwrap
]: New lint. This is the same as or_then_unwrap but for or_else calls with a closure immediately callingSome
.