Skip to content

needless_borrow false negative on fn that takes AsRef<..> and gets &var #6810

@matthiaskrgr

Description

@matthiaskrgr

Lint name: needless_borrow

I tried this code:

I wonder if the lint should warn on passing &arg to a function that takes AsRef<..> where the & would not be needed

#![warn(clippy::needless_borrow)]

pub fn main() {
    fn is_hello<T: AsRef<str>>(s: T) {
        assert_eq!("hello", s.as_ref());
    }
    let s: &String = &"hello".to_string();

    is_hello(&&&&s); // warn?
    is_hello(&s); // warn?
    is_hello(s); // ok
    is_hello(&"hello"); // warn?
    is_hello("hello"); // ok
}

Meta

  • cargo clippy -V: clippy 0.1.52 (9c09c1f 2021-02-26)
  • rustc -Vv:
rustc 1.52.0-nightly (9c09c1f7c 2021-02-26)
binary: rustc
commit-hash: 9c09c1f7cfcf9de0522bcd1cfda32b552195c464
commit-date: 2021-02-26
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions