Skip to content

Needless borrow lint #884

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

Merged
merged 3 commits into from
May 9, 2016
Merged

Needless borrow lint #884

merged 3 commits into from
May 9, 2016

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Apr 26, 2016

This lint can be very annoying if it has false positives (which my first naive version had). So give me suggestions to run this on :)

Crates without false positives:

  • clippy
  • cargo
  • racer
  • serde
  • serde_codegen

Crates to test:

Crates with false positives:

if let ExprAddrOf(MutImmutable, ref inner) = e.node {
if let TyRef(..) = cx.tcx.expr_ty(inner).sty {
let ty = cx.tcx.expr_ty(e);
let adj_ty = cx.tcx.expr_ty_adjusted(e);
Copy link
Member

@mcarton mcarton Apr 27, 2016

Choose a reason for hiding this comment

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

There are other kinds of adjustment, rustc::ty::Tables::adjustments + AdjustDerefRef would maybe be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's relevant, because I'm testing for & ops on ref types, any adjustments that can happen, will still happen when you remove the & op, because it's already a ref type.

Copy link
Contributor Author

@oli-obk oli-obk Apr 27, 2016

Choose a reason for hiding this comment

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

In fact, I think that might decrease the usefulness of this lint. Because then

let x: &[u8] = &&Vec::new();

would not trigger anymore, even though it should be

let x: &[u8] = &Vec::new();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also test if the adjusted type of inner is the same as the adjusted type of e

@oli-obk
Copy link
Contributor Author

oli-obk commented May 2, 2016

rebased

1 similar comment
@oli-obk
Copy link
Contributor Author

oli-obk commented May 2, 2016

rebased

@llogiq
Copy link
Contributor

llogiq commented May 6, 2016

@mcarton LGTM, but since you reviewed, I'd like to know if you have any issue that should be resolved before merging.

@oli-obk
Copy link
Contributor Author

oli-obk commented May 9, 2016

rebased

@mcarton
Copy link
Member

mcarton commented May 9, 2016

@oli-obk I’m ready to merge but I just don’t like the name of the lint, needless_ref makes me think of the ref keyword, not the & operator. What do you think of needless_borrow?

@oli-obk
Copy link
Contributor Author

oli-obk commented May 9, 2016

sgtm

@oli-obk
Copy link
Contributor Author

oli-obk commented May 9, 2016

although sometimes it can be either ;)

@oli-obk
Copy link
Contributor Author

oli-obk commented May 9, 2016

do we have a lint that lints on using ref for Copy types?

@oli-obk oli-obk changed the title Needless ref lint Needless borrow lint May 9, 2016
@mcarton mcarton merged commit 488199d into rust-lang:master May 9, 2016
@oli-obk oli-obk deleted the needless_ref2 branch May 9, 2016 11:17
@llogiq
Copy link
Contributor

llogiq commented May 9, 2016

Good work everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants