Skip to content

Detect unused "passing self into closure" pattern to side-step borrow checker #109271

Closed
@estebank

Description

@estebank

Code

.

Current output

error[E0500]: closure requires unique access to `*self` but it is already borrowed
   --> compiler/rustc_resolve/src/macros.rs:828:25
    |
824 |                       self.visit_scopes(
    |                       -    ------------ first borrow later used by call
    |  _____________________|
    | |
825 | |                         ScopeSet::Macro(MacroKind::Derive),
826 | |                         &parent_scope,
827 | |                         ident.span.ctxt(),
828 | |                         |this, scope, use_prelude, ctxt| {
    | |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ closure construction occurs here
...   |
839 | |                                                     let x = self.get_macro_by_def_id(def_id);
    | |                                                             ---- second borrow occurs due to use of `*self` in closure
...   |
861 | |                         },
862 | |                     );
    | |_____________________- borrow occurs here

Desired output

error[E0500]: closure requires unique access to `*self` but it is already borrowed
   --> compiler/rustc_resolve/src/macros.rs:828:25
    |
824 |                       self.visit_scopes(
    |                       -    ------------ first borrow later used by call
    |  _____________________|
    | |
825 | |                         ScopeSet::Macro(MacroKind::Derive),
826 | |                         &parent_scope,
827 | |                         ident.span.ctxt(),
828 | |                         |this, scope, use_prelude, ctxt| {
    | |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ closure construction occurs here
...   |
839 | |                                                     let x = self.get_macro_by_def_id(def_id);
    | |                                                             ----
    | |                                                             |
    | |                                                             second borrow occurs due to use of `*self` in closure
    | |                                                             help: a closure argument with the same type as `self` is available, you might have meant to use that: `this`
...   |
861 | |                         },
862 | |                     );
    | |_____________________- borrow occurs here

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsD-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions