Add lints for discouraging eager / lazy function arguments#17108
Conversation
8c0bb35 to
c58fafc
Compare
This comment has been minimized.
This comment has been minimized.
c58fafc to
3e35de4
Compare
3e35de4 to
7f7343b
Compare
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Jarcho (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
☔ The latest upstream changes (possibly #17164) made this pull request unmergeable. Please resolve the merge conflicts. |
| fn check_fn( | ||
| &mut self, | ||
| cx: &LateContext<'tcx>, | ||
| _: FnKind<'_>, | ||
| _: &FnDecl<'_>, | ||
| body: &'tcx Body<'_>, | ||
| _: Span, | ||
| _: LocalDefId, | ||
| ) { | ||
| let mut visitor = DiscouragedLazyEvaluationVisitor { cx }; | ||
| walk_body(&mut visitor, body); | ||
| } |
| if let ExprKind::Call(func, args) = expr.kind | ||
| && let ExprKind::Path(ref qpath) = func.kind | ||
| && let Some(def_id) = self.cx.qpath_res(qpath, func.hir_id).opt_def_id() |
There was a problem hiding this comment.
This doesn't handle method calls. Calls to functions without arguments should also not be checked any further.
| }) | ||
| .collect(); | ||
|
|
||
| if !lazy_args.is_empty() { |
There was a problem hiding this comment.
Why collect just to check if it's empty. This is what any is for.
|
Reminder, once the PR becomes ready for a review, use |
|
These attributes should name the alternative function in a way that allows us to find it. We can't correctly trigger the lint if we don't know which arguments to check, so at a minimum those need to be labelled as well. If we can lookup the alternative function then we can auto-detect which argument(s) is changing base on the types involved. |
|
@rustbot label lint-nominated |
|
This lint has been nominated for inclusion. |
Closes #13678
Basically what
or_fun_callandunnecessary_lazy_evalalready do, except controlled by an attribute that can be added to any function by downstream library authors. Two major use cases that immediately come to mind areanyhow's.contextvs.with_context, andeyre's.wrap_errvs..wrap_err_with.Refer to the docs in this PR for some more detailed explanation, but the basic gist is that library authors will be able to do something like this:
Also a thought for the future: These lints could potentially replace
or_fun_callandunnecessary_lazy_evalaltogether if the corresponding attributes were added to the relevant methods inOptionandResult. I've thought about simply amending the two, but didn't want to potentially break backwards compatibility.Todo:
changelog: Adds new lints
eager_fun_callanddiscouraged_lazy_evaluationthat check for calling functions with eagerly evaluated arguments, and unnecessary closures used to produce arguments respectively.