Skip to content

Warn when a borrow expression is unused #76264

Closed
@ecstatic-morse

Description

@ecstatic-morse

Borrowing an expression is side-effect free, so there should never be a reason to write the first instead of the second.

&expr;
// vs
expr;

If you really wanted to do this you could assign the result to an unused variable:

let _ = &expr;

Therefore, we should add a lint for unused borrows to the compiler. Why is this important? A misplaced semi-colon before an && operator in a multi-line boolean expression causes subsequent clauses to be silently ignored.

let trex = TyrannosaurusRex::new();
let is_a_dog = has_a_tail(trex)
  && has_bad_breath(trex)
  && is_a_carnivore(trex); // Misplaced semi-colon (perhaps due to reordering of lines)
  && is_adorable(trex);

if is_a_dog {
    give_hug(trex); // Ouch!
}

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.D-papercutDiagnostics: An error or lint that needs small tweaks.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-help-wantedCall for participation: Help is requested to fix this issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions