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: useless_asref when used to go from &&T to &T #12785

Open
mqudsi opened this issue May 9, 2024 · 1 comment
Open

False positive: useless_asref when used to go from &&T to &T #12785

mqudsi opened this issue May 9, 2024 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@mqudsi
Copy link

mqudsi commented May 9, 2024

Summary

Normally, &&T will automatically and silently behave like &T and using foo.map(|t| t.as_ref()) is unneeded. However the useless_asref lint has a false positive in the case where types need to line up, e.g. the return types of if and else, the arms of match brackets, or in this case, Option<T> and the result of Option::unwrap_or(...).

Lint Name

useless_asref

Reproducer

I tried this code:

    pub fn has_libraries(&self, libraries: &[&str]) -> bool {
        let stub = libraries
            .first()
            .map(|l| l.as_ref())
            .unwrap_or("has_libraries");
        // ...
    }

I saw this happen:

warning: this call to `as_ref` does nothing
   --> src/lib.rs:544:22
    |
544 |             .map(|l| l.as_ref())
    |                      ^^^^^^^^^^ help: try: `l`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_asref
    = note: `#[warn(clippy::useless_asref)]` on by default

I expected to see this happen:

The useless_asref lint is incorrect here since without the call rustc (1.80.0-nightly) will complain about mismatched types:

error[E0308]: mismatched types
   --> src/lib.rs:545:24
    |
545 |             .unwrap_or("has_libraries");
    |              --------- ^^^^^^^^^^^^^^^ expected `&&str`, found `&str`
    |              |
    |              arguments to this method are incorrect
    |
    = note: expected reference `&&_`
               found reference `&'static _`

Version

rustc 1.80.0-nightly (faefc618c 2024-05-07)
binary: rustc
commit-hash: faefc618cf48bd794cbc808448df1bf3f59f36af
commit-date: 2024-05-07
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4

Additional Labels

@rustbot label +I-suggestion-causes-error

@mqudsi mqudsi added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 9, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label May 9, 2024
@mqudsi
Copy link
Author

mqudsi commented May 9, 2024

In this case, clippy should have actually suggested replacing .map(|l| l.as_ref()) with .copied(), which is clearer, doesn't trigger any other clippy lints, and is actually better for the optimizer (though this option is not necessarily always available).

mqudsi added a commit to mqudsi/rsconf that referenced this issue May 9, 2024
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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants