Skip to content
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

Lint for unused borrows as part of UNUSED_MUST_USE #76894

Closed

Conversation

ecstatic-morse
Copy link
Contributor

@ecstatic-morse ecstatic-morse commented Sep 18, 2020

Resolves #76264.

This means an unused_must_use warning for statements like &expr; and &mut expr;. unused_must_use was chosen because it already lints for logical operators (&& and ||) whose result is unused. Unused borrows actually appear fairly often in rustc's test suite, since we have tests that rely on side-effects of the index operator (panicking). These cannot be written as expr[..];, since the result is unsized, but they can be written as let _ = &expr[..];, which is what this PR does.

See the linked issue for the motivating example. This lint also found a benign mistake in the derived impl of HashStable.

@rust-highfive
Copy link
Collaborator

r? @lcnr

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 18, 2020
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

does this need lang or compiler signoff considering that this is a userfacing change?

@matthiaskrgr
Copy link
Member

How similar is this to clippys needless_borrow lint?
https://rust-lang.github.io/rust-clippy/master/#needless_borrow

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 18, 2020

I think it needs @rust-lang/lang approval, but I'm not 100% sure.

@matthiaskrgr Sounds similar. I don't use clippy.

@Mark-Simulacrum Mark-Simulacrum added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Sep 18, 2020
@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 18, 2020

Ah, so the transformation from &expr; to expr; isn't always correct. If expr returns an unsized value like a slice, it will not compile (without unsized rvalues anyways). I think we generally don't have lints in the compiler with false positives. However, I would really like to fix the pathological case in #76264 (reproduced below), a bare &x[0..4]; should be pretty rare outside of the compiler's test suite and we can suggest let _ = &expr; in this situation.

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!
}

There are various alternatives with no false positives: Only triggering on && comes immediately to mind. I'll wait to see how others feel.

@lcnr
Copy link
Contributor

lcnr commented Sep 19, 2020

If expr returns an unsized value like a slice, it will not compile (without unsized rvalues anyways).

Afaict the exprs which do so are the following:

&slice[..];
&y[0];
&*boxed;

all of which seem like something we can (and should) warn on, even if the suggestion is incorrect.

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 19, 2020

For my own clarity, postfix indexing (a[idx]) desugars to *std::ops::Index{,Mut}::index{,_mut}(&{,mut }a, idx) unless a is a slice or an array and idx is a usize, so conceptually the only problem is reborrows (&*).

Also, I'm starting to think this should be folded into UNUSED_MUST_USE. The logical boolean operators are already included in that lint, and they have a similar "false-positive" problem, since someone could put a function with side-effects in the second half (e.g. condition || panic!()). The suggestion there is the same as for unused borrows: Assign the result to _.

@ecstatic-morse ecstatic-morse force-pushed the lint-unused-borrows branch 4 times, most recently from 1693ffb to 2c56247 Compare September 21, 2020 18:46
@ecstatic-morse ecstatic-morse changed the title Warn-by-default lint for unused borrows Lint for unused borrows as part of UNUSED_MUST_USE Sep 24, 2020
@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 24, 2020

This is now part of unused_must_use, and I've updated the title and description. This is just waiting on @rust-lang/lang to weigh in. I'll nominate it for the meeting as well.

@scottmcm
Copy link
Member

scottmcm commented Sep 24, 2020

This makes sense to me. Since we lint on !x;, we might as well lint on &x too.

I don't know whether this needs a full FCP (we can talk about it on Monday), but in case it does,
@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Sep 24, 2020

Team member @scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 24, 2020
@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 24, 2020

I don't know whether this needs a full FCP (we can talk about it on Monday), but in case it does,

I definitely approved a similar PR (#74869) without lang team approval. lcnr asked about the process in #76894 (review), and it occurred to me that I didn't actually know. If the lang-team does want to weigh in on this, they should consider it in concert with #69466, which also involves side-effects and UNUSED_MUST_USE. Y'all might have better things to do, however.

@withoutboats
Copy link
Contributor

@rfcbot concern crater

Enthusiastic about this intuitively, but I'd like a crater run to make sure that this isn't going to suddenly lint a large part of the ecosystem or anything.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 27, 2021
… r=lcnr

Lint for unused borrows as part of `UNUSED_MUST_USE`

Resolves rust-lang#76264.

This means an `unused_must_use` warning for statements like `&expr;` and `&mut expr;`. `unused_must_use` was chosen because it already lints for logical operators (`&&` and `||`) whose result is unused. Unused borrows actually appear fairly often in `rustc`'s test suite, since we have tests that rely on side-effects of the index operator (panicking). These cannot be written as `expr[..];`, since the result is unsized, but they can be written as `let _ = &expr[..];`, which is what this PR does.

See the linked issue for the motivating example. This lint also found a benign mistake in the derived impl of `HashStable`.
@Dylan-DPC-zz
Copy link

@bors r-

failed in rollup

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 27, 2021
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Mar 4, 2021
@crlf0710 crlf0710 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 20, 2021
@Dylan-DPC-zz
Copy link

@ecstatic-morse any updates on this?

bors bot added a commit to taiki-e/pin-project that referenced this pull request Mar 28, 2021
322: v0.4: Fix unused_must_use warning on unused borrows r=taiki-e a=taiki-e

This fixes `unused_must_use` warning on unused borrows, which will be added to rustc in the future.

See rust-lang/rust#76894 fore more.

(Note: pin-project 1.0 does not have this problem.)


Co-authored-by: Taiki Endo <[email protected]>
bors bot added a commit to taiki-e/pin-project that referenced this pull request Mar 28, 2021
322: v0.4: Fix unused_must_use warning on unused borrows r=taiki-e a=taiki-e

This fixes `unused_must_use` warning on unused borrows, which will be added to rustc in the future.

See rust-lang/rust#76894 fore more.

(Note: pin-project 1.0 does not have this problem.)


Co-authored-by: Taiki Endo <[email protected]>
@crlf0710 crlf0710 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 9, 2021
@crlf0710 crlf0710 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 1, 2021
@crlf0710 crlf0710 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 22, 2021
@lcnr
Copy link
Contributor

lcnr commented May 24, 2021

I am not able to review any PRs in the near future.

r? @RalfJung

@rust-highfive rust-highfive assigned RalfJung and unassigned lcnr May 24, 2021
@RalfJung
Copy link
Member

Given that @ecstatic-morse has been inactive here for months and in general does not seem to be doing much Rust work any more, I'm going to close this PR for now.
@ecstatic-morse if you (or anyone else) wants to pick this up again, feel free to reopen. :)

@RalfJung RalfJung closed this May 24, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jun 19, 2021
…=Aaron1011

Lint for unused borrows as part of UNUSED_MUST_USE

close rust-lang#76264

base on rust-lang#76894

r? `@RalfJung`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 19, 2021
…aron1011

Lint for unused borrows as part of UNUSED_MUST_USE

close rust-lang#76264

base on rust-lang#76894

r? `@RalfJung`
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Jul 1, 2021
Lint for unused borrows as part of UNUSED_MUST_USE

close rust-lang/rust#76264

base on rust-lang/rust#76894

r? `@RalfJung`
naturallymitchell pushed a commit to naturallymitchell/fuchsia-storage that referenced this pull request Jun 10, 2022
Unused references have become a build warning in the rust compiler and
as warning are errors in our build, this is blocking the rust toolchain
from rolling.
See rust-lang/rust#76894

Change-Id: Ifcbdcc93efa4ca53003c99515767b58dc3675194
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/545184
Commit-Queue: Adrian Danis <[email protected]>
Fuchsia-Auto-Submit: Adrian Danis <[email protected]>
Reviewed-by: Chris Suter <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn when a borrow expression is unused