Skip to content
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

Closed
llogiq opened this issue Aug 21, 2015 · 8 comments · Fixed by #8271
Closed

false positive on ptr_arg against &Vec<_> #214

llogiq opened this issue Aug 21, 2015 · 8 comments · Fixed by #8271
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have T-AST Type: Requires working with the AST T-middle Type: Probably requires verifiying types

Comments

@llogiq
Copy link
Contributor

llogiq commented Aug 21, 2015

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); }

@llogiq llogiq added C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-AST Type: Requires working with the AST T-middle Type: Probably requires verifiying types labels Aug 21, 2015
@birkenfeld
Copy link
Contributor

I don't think you can call push on an immutable reference :) There's currently only one method on Vec<T> that isn't on [T] that doesn't require a mutable reference: capacity. So I think we can with good conscience keep that.

But if the lint currently also warns on &mut Vec<T> or &mut String, it should definitely not do that.

@birkenfeld
Copy link
Contributor

I'll do a PR for that.

birkenfeld added a commit to birkenfeld/rust-clippy that referenced this issue Aug 21, 2015
* do not trigger on mutable references
* use "real" type from ty, not AST type
birkenfeld added a commit to birkenfeld/rust-clippy that referenced this issue Aug 21, 2015
* do not trigger on mutable references
* use "real" type from ty, not AST type
@vnermolaev
Copy link

vnermolaev commented Apr 20, 2020

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

warning: writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used with non-Vec-based slices.
 --> src/main.rs:1:12
  |
1 | fn test(v: &Vec<u8>) -> Result<(), &'static str> {
  |            ^^^^^^^^ help: change this to: `&[u8]`
  |
  = note: `#[warn(clippy::ptr_arg)]` on by default

Changing it to &[u8] expectedly breaks the compilation due to Vec::contains.

cargo clippy -V
clippy 0.0.212 (4ee12063 2020-02-01)

@birkenfeld
Copy link
Contributor

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 #[allow(clippy::ptr_arg)] on the function.

@vnermolaev
Copy link

vnermolaev commented Apr 21, 2020

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.
It is perhaps worth to add a note in the docs.

@birkenfeld
Copy link
Contributor

Agreed!

@flip1995 flip1995 reopened this Apr 21, 2020
@flip1995 flip1995 added A-documentation Area: Adding or improving documentation good-first-issue These issues are a good way to get started with Clippy labels Apr 21, 2020
@Ryan1729
Copy link
Contributor

Ryan1729 commented Aug 10, 2020

When I first saw the example above, I wondered why Vec::contains couldn't be written in such a way that the given case works. After a bit of searching I found rust-lang/rust#62367 which contains a suggestion that the restriction was a mistake in the initial implementation of Vec::contains, which cannot be corrected since it would cause type inference regressions. Note that Vec::contains currently just directly calls slice::contains.

bors added a commit that referenced this issue Aug 12, 2020
Add example of false positive to PTR_ARG docs.

Addresses #214

changelog: Add example of false positive to `ptr_arg` docs.
@bors bors closed this as completed in 3e3e50b Aug 12, 2020
@flip1995
Copy link
Member

Keeping this open, because only the documentation was added.

@flip1995 flip1995 reopened this Aug 12, 2020
@flip1995 flip1995 removed A-documentation Area: Adding or improving documentation good-first-issue These issues are a good way to get started with Clippy labels Aug 12, 2020
Ryan1729 added a commit to Ryan1729/rust-clippy that referenced this issue Aug 12, 2020
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 21, 2020
@bors bors closed this as completed in 4992548 Jan 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have T-AST Type: Requires working with the AST T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants