Skip to content

diagnostics: rust could suggest removing reference here #76703

@matthiaskrgr

Description

@matthiaskrgr

I was working on some clippy code and had something along the following:

/// lint for length-1 `str`s as argument for `insert_str`
fn lint_single_char_insert_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
    let _pos_arg = snippet_2(&cx, args[1].span, ".."); // here we need "cx" instead of "&cx" !
}

pub fn snippet_2<'a, T: LintContext>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> {
    todo!();
}

This did not compile:

error[E0277]: the trait bound `&LateContext<'_>: LintContext` is not satisfied
    --> clippy_lints/src/methods/mod.rs:3346:20
     |
3346 |     let _pos_arg = snippet_2(&cx, args[1].span, "..");
     |                    ^^^^^^^^^ the trait `LintContext` is not implemented for `&LateContext<'_>`
...
3349 | pub fn snippet_2<'a, T: LintContext>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> {
     |                         ----------- required by this bound in `snippet_2`
     |
     = help: the following implementations were found:
               <LateContext<'_> as LintContext>

The problem was that snippet_2 needed cx instead of &cx. It would be great if rustc could suggest to remove the reference here.
I tried to write a small sample code from scratch but in all of these cases, rustc managed to correctly suggest to remove the &, so there must be some extra aspects here. :/

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-papercutDiagnostics: An error or lint that needs small tweaks.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions