-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New Lint: detect unused &mut
references in function parameters
#5546
Comments
This would be an entirely new lint. Detecting if a mutable reference is actually necessary is way harder, than just detecting if a mutable reference is given to a function, that only takes a shared reference. |
&mut
references in function parameters
@flip1995 can't |
Sadly not. |
@flip1995 why? Sorry, I'm not familiar with clippy's internals. |
Maybe a relevant info before the explanation: Clippy doesn't do the compilation. Clippy just calls rustc programmatically and uses the Clippy can only run one compiler pass. Doing something like this would mean that Clippy has to modify a file on disk and then run the compiler again, and after that revert the modification of the file. Perf concerns aside (we'd probably have to do this multiple times for multiple lints), this would be unexpected behavior of a linter from the users perspective. At least I wouldn't expect that my static analyzer modifies files on disk. Another option might be to only re-compile the function. But that wouldn't work because we'd had to manually rerun every Don't be sorry when asking questions. It's the best way to learn :) |
@flip1995 thanks for a detailed explanation. You are concerned that modifying source code files might confuse the user. What prevents |
Clippy would still create files on disk (in I like to be strict here and keep the behavior that Clippy doesn't do anything regarding the file system. (Except for the |
In unsafe code, For example: struct LeakyBox<T>(*mut T);
impl<T> LeakyBox<T> {
pub fn new(v: T) -> Self {
Self(Box::into_raw(Box::new(v)))
}
pub fn get_mut(&self) -> &mut T {
unsafe { &mut *self.0 }
}
} This code compiles fine and |
This lint eixsts now: https://rust-lang.github.io/rust-clippy/master/index.html#/needless_pass_by_ref_mut , implemented in #10900 Output for the reproducer above:
|
Hi! Clippy in the Rust Playground spots this problem:
But it doesn't find the inverted problem here:
While this is caught by Rustc too:
The text was updated successfully, but these errors were encountered: