Implement Interchangeable_params lint#16973
Conversation
|
rustbot has assigned @samueltardieu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for 9d6ccc8
This comment will be updated if you push new changes |
There was a problem hiding this comment.
The lint is not ready yet, as it has many issues:
- It suggests creating
structin animplblock, which is not possible. Even if the structs were created outside theimplblock, the lint would fail on things likeas it would suggest wrappingimpl Foo { fn f(self, other1: &Self, other2: &Self) {} }
Self, which is not possible outside theimpl. The situation would also be identical with any dependent type, or with generic parameters. - It will suggest creating new
structfrom the parameter's capitalized name, which may conflict with the existing types, as inwhich would fail on the recursivefn f(string: String, a: String);
struct String(String);type definition. - Generic parameters cannot be used outside the function definition. For example,
would suggest
fn do_nothing<T>(a: T, b: T) {}
which is not valid Rust.struct X(T); struct Y(T); fn do_nothing<T>(x: X, y: Y) {}
Also, in general, this lint does too many snippet manipulation and string creation. It would probably be better to use the clippy_utils::source functions to get references to the source, and bail out if any snippet cannot be obtained.
Parsing for { or ( might also not be robust in presence of inline comments inside the function definiiton.
| } | ||
| // get the span for the function declaration part (,minus the body) | ||
| let fnspan = if let Some(snippet) = snippet_opt(cx, span) { | ||
| let snippet = snippet.split('{').nth(0).unwrap_or("").trim_end(); |
There was a problem hiding this comment.
Using .split_once() would avoid unnecessary work. Also, this seems to be used to get the indentation only, which should be done only when emitting the lint and doesn't require manipulating the span.
Also, in which case wouldn't the snippet contain {? In this case, wouldn't it be better to bail out from the lint?
| span | ||
| }; | ||
| // and the part of the line before the arguments | ||
| let fnstart = if let Some(snippet) = snippet_opt(cx, span) { |
There was a problem hiding this comment.
Ditto, only used when emitting the lint, should be deferred. And same question about bailing out if snippet_opt() returns None.
| }; | ||
| // and the part of the line before the arguments | ||
| let fnstart = if let Some(snippet) = snippet_opt(cx, span) { | ||
| let snippet = snippet.split('(').nth(0).unwrap_or("").trim_end(); |
There was a problem hiding this comment.
Again, split_once() would be better.
| cx, | ||
| INTERCHANGABLE_PARAMS, | ||
| fnspan, | ||
| "interchangable paramters", |
There was a problem hiding this comment.
Two typos: "interchangeable parameters". The message is not clear enough though.
| let mut names: Vec<String> = Vec::new(); | ||
| // first get the list of types. | ||
| for argtype in fndecl.inputs { | ||
| let mut snip = (*snippet(cx, argtype.span, "..")).to_string(); |
There was a problem hiding this comment.
Why use the textual representation instead of the TyKind? Wouldn't that be possible?
|
Reminder, once the PR becomes ready for a review, use |
|
Thank you for your feedback. |
| declare_lint_pass!(InterchangableParams => [INTERCHANGABLE_PARAMS]); | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for InterchangableParams { | ||
| fn check_fn( |
There was a problem hiding this comment.
This currently lints in closures, as seen in:
warning: interchangable paramters
--> target/lintcheck/sources/ahash-0.8.11/src/random_state.rs:265:19
|
265 | let mix = |l: u64, r: u64| {
| ^^^^^^^^^^^^^^^^
|
help: consider writing
|
265 ~ let mix = struct L(u64);
266 + struct R(u64);
267 + /* .. */
268 + |l: u64, r: u64| {
269 ~ let mut h = hasher.clone(l: L, r: R) {
|
Closure are most often just used as local helpers, so I'd argue that adding newtypes for their parameters isn't really worth it.
65f590a to
3d0f880
Compare
|
Not ready for primetime yet. Changed strategy, detects impl functions and closures. Not making suggestions yet. |
3a84457 to
d81c12a
Compare
b62f943 to
264f71c
Compare
This comment has been minimized.
This comment has been minimized.
43d618d to
06b5535
Compare
This comment has been minimized.
This comment has been minimized.
ca0f923 to
307cf87
Compare
307cf87 to
32dc2ff
Compare
32dc2ff to
db73821
Compare
changelog: [
interchangable_params]: Add interchangable_params lintFixes #16735
This PR implements issue #16735, which suggests a new lint that examines the types of parameters of a function and suggests that any repeated types get newtype values. Doing this will help prevent accidentally changing the order of the parameters by helping the compiler detect the issue.