Skip to content

Lint against useless &mut (i.e., uplift clippy::unnecessary_mut_passed) #71493

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

Open
matklad opened this issue Apr 23, 2020 · 2 comments
Open
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@matklad
Copy link
Member

matklad commented Apr 23, 2020

In rust-analyzer, I had the following code:

impl Completions {
    pub(crate) fn add_field(
        &mut self,
        ctx: &CompletionContext,
        field: hir::StructField,
        ty: &Type,
    ) {
        let is_deprecated = is_deprecated(field, ctx.db);
        let mut completion_item = CompletionItem::new(CompletionKind::Reference)
            .kind(CompletionItemKind::Field)
            .build();

        compute_score(&mut completion_item, ctx);

        self.add(completion_item);
    }
}

pub(crate) fn compute_score(
    completion_item: &mut CompletionItem, 
    ctx: &CompletionContext,
) {
   ...
}

I've changed the compute_score function to

pub(crate) fn compute_score(
    completion_item: &CompletionItem, 
    ctx: &CompletionContext,
) -> Option<CompletionScore> {
   ...
}

After that, the code compiled without warnings, was broken, because compute_score(&mut completion_item, ctx); statement no longer did what it was supposed to be doing.

The smell here I think is that we use explicit &mut, which gets silently reborrowed to &. I think we should lint against, roughly, all programs where &mut e expression can be replaced with &e without breaking the compilation.

@matklad matklad added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Apr 23, 2020
@jonas-schievink jonas-schievink added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Apr 23, 2020
@fmease fmease changed the title Lint against useless &mut Lint against useless &mut (i.e., uplift clippy::unnecessary_mut_passed) Feb 11, 2025
@fmease
Copy link
Member

fmease commented Feb 11, 2025

If this is about code like drop::<&str>(&mut String::new()), then this is covered by Clippy already, namely by clippy::unnecessary_mut_passed which was added 10 years ago.

Are you requesting to uplift the Clippy lint into rustc? I'm not sure if that's super warranted. Related: clippy::needless_borrow (drop::<&str>(&"")).

@matklad
Copy link
Member Author

matklad commented Feb 11, 2025

Yes, I suggest uplifting the lint to rustc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants