-
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
false positive on ptr_arg
against &Vec<_>
#214
Comments
I don't think you can call But if the lint currently also warns on |
I'll do a PR for that. |
* do not trigger on mutable references * use "real" type from ty, not AST type
* do not trigger on mutable references * use "real" type from ty, not AST type
The issue still persists when a reference on a vector is passed for code that uses immutable vector methods. Please see below or in (playground) fn test(v: &Vec<u8>) -> Result<(), &'static str> {
// Complicated logic to retrieve values from db.
let from_db = vec![vec![1, 2], vec![3, 4]];
if !from_db.contains(v) {
return Err("Not present");
}
Ok(())
}
fn main() -> Result<(), &'static str> {
let v: Vec<u8> = vec![5, 6];
test(&v)
} I does compile and run, but if I lint it with Clippy, it will complain
Changing it to
|
IMO this is a rare case, and we can't do the sort of checks that would catch this false positive. At the same time, this lint is a very useful one since it teaches newbies about slices and coercion. So in this case, the best solution is to put |
Indeed, I addressed the issue how you indicated, but thought it would be valuable to showcase this scenario. Mainly because that following Clippy's advice breaks the compilation. |
Agreed! |
When I first saw the example above, I wondered why |
Add example of false positive to PTR_ARG docs. Addresses #214 changelog: Add example of false positive to `ptr_arg` docs.
Keeping this open, because only the documentation was added. |
It's not possible to call any of
Vec
's methods on a slice, whether mutable or not. So if any method of Vec is called, the lint message has to be considered a false positive.Example:
fn foo(v: &Vec<Name>, n: Name) { v.push(n); }
The text was updated successfully, but these errors were encountered: