-
Notifications
You must be signed in to change notification settings - Fork 1.6k
unwrap_used
, expect_used
: accept macro result as receiver
#14575
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
Conversation
Lintcheck shows many new hits, as expected (mostly with |
clippy_lints/src/methods/mod.rs
Outdated
pub fn method_call_expansion_ok<'tcx>( | ||
recv: &'tcx Expr<'tcx>, | ||
) -> Option<(&'tcx str, &'tcx Expr<'tcx>, &'tcx [Expr<'tcx>], Span, Span)> { | ||
if let ExprKind::MethodCall(path, receiver, args, call_span) = recv.kind { | ||
let name = path.ident.name.as_str(); | ||
return Some((name, receiver, args, path.ident.span, call_span)); | ||
} | ||
None | ||
} |
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.
This doesn't really add anything over just matching inline.
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 idea was to make it easy for some lints use the pattern
if let Some((_, outer_receiver, _, _)) = method_call(expr) {
if let Some((_, inner_receiver, _)) = method_call_expansion_ok(outer_receiver) {
…
}
}
since in some cases they wouldn't care if the inner_receiver was a macro call.
For example, we could easily (I just made the check) warn on
macro_rules! iter { ($e:expr) => { $e.iter() }; }
let _ = iter!([String::new()]).cloned().count();
with
error: unneeded cloning of iterator items
--> /tmp/t.rs:6:13
|
6 | let _ = iter!([String::new()]).cloned().count();
| ^^^^^^^^^^^^^^^^^^^^^^---------^^^^^^^^
| |
| help: remove
Just by replacing a call to method_call()
in methods/mod.rs
with a call to method_call_expansion_ok()
, and using the right span when printing the error message. There are really no good reasons not to lint when the receiver of .cloned()
comes from expansion here, and this function would facilitate that.
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.
if let Some((...)) = method_call_of_expansion(e)
isn't any simpler than if let ExprKind::Method(...) = e.kind
. method_call
actually does something since it also filters the result.
Ping @samueltardieu. |
104a217
to
7f03532
Compare
Change done |
clippy_lints/src/methods/mod.rs
Outdated
/// This ensures that neither the receiver nor any of the arguments | ||
/// come from expansion, contrary to [`method_call_expansion_ok()`]. |
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.
This is now outdated.
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.
Argh, fixed.
7f03532
to
999db5c
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.
Thank you.
changelog: [
unwrap_used
,expect_used
]: lint even when the receiver is a macro expansion resultThis also paves the way for expanding more method call lints to expanded receivers or arguments.
Fixes #13455