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

feat(lint): deref and deref_mut method explicit calls #3258

Closed

Conversation

tommilligan
Copy link
Contributor

Instead of calling x.deref() or x.deref_mut(), &*x and &mut *x should be recommended instead.

Probably should not lint if it's part of a method chain

This PR adds a lint deref_method_explicit (all, complexity), which fulfils the above criteria. Matching ui test and output have also been added.

I'm not certain how best to handle macro expansions as part of the lint - if a macro contains a deref() call for instance. If someone could provide a failing testcase for me to work against that would be great.

Closes #1566

@ghost
Copy link

ghost commented Oct 5, 2018

Maybe this should be a pedantic lint to be consistent with explicit_iter_loop and explicit_into_iter_loop. See #3063 and #3119.

@tommilligan
Copy link
Contributor Author

tommilligan commented Oct 5, 2018

@mikerite thanks for the feedback. I'll also rename to explicit_deref_method for consistency.

Could you advise on why travis-ci and appveyor disagree, and which to trust? The issue appears to be a fix for my local build failing when I first cloned the repo:

858a902#diff-5b47b8bbc8833f6268e936ea8682644dL93

edit: updated my local nightly version

@flip1995
Copy link
Member

flip1995 commented Oct 5, 2018

Travis and appveyor check if clippy compiles with rustc master, not with rustc nightly. You can check this locally with

rustup-toolchain-install-master -f -n master
cargo +master test

See https://github.com/rust-lang-nursery/rust-clippy/blob/master/CONTRIBUTING.md#fixing-build-failures-caused-by-rust

Currently Travis fails because of

+++./util/update_lints.py -c
Please run util/update_lints.py to regenerate lints lists.

(This only gets checked in travis)

Also could you leave the requested comment in #3230, after this gets merged?

/// let c = a.deref_mut();
///
/// // excludes
/// let e = d.deref().unwrap();
Copy link
Member

@flip1995 flip1995 Oct 5, 2018

Choose a reason for hiding this comment

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

Does your lint exclude these cases? Since _.deref().len() gets linted, I have a feeling, that also _.unwrap() will get linted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quite correct - should be the other way round. d.unwrap().deref() would not be linted.

Copy link
Member

Choose a reason for hiding this comment

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

Could you also add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Oh yeah, I missed that, thanks!

@flip1995
Copy link
Member

flip1995 commented Oct 5, 2018

This lint produces suggestions, that lead to erroneous code: Playground

@tommilligan
Copy link
Contributor Author

Hmm, thanks for catching. Technically, we could suggest (&mut *a).len(); but that looks awful.

I need to alter the linting to only pick up on deref when it's the last method, rather than the first in the chain. I think this will require a Visitor implementation, so I'll look into that unless there are other suggestions.

@flip1995
Copy link
Member

ping @tommilligan

If you don't have the motivation to fix the suggestion (this can take a while to do), you can replace the span_lint_and_sugg functions with
https://github.com/rust-lang-nursery/rust-clippy/blob/319b75c75b65985b7d3389d65a49f993ef3345fa/clippy_lints/src/utils/mod.rs#L522-L528
and this lint would be good to go. Having a lint without a suggestion is way better than not having the lint at all!

span_help_and_lint is not autofixable by rustfix, so having erroneous suggestions there does not really matter.

@flip1995 flip1995 added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Oct 24, 2018
@tommilligan
Copy link
Contributor Author

@flip1995 thanks for the ping. I believe my current implementation is actively unhelpful in some circumstances, so I'm not happy releasing it into the wild as is. I'll try to come back and revisit soon

@flip1995
Copy link
Member

Ping from triage @tommilligan. Do you want to continue working on this?

@flip1995
Copy link
Member

Ping from triage @tommilligan: It looks like this PR hasn't received any updates in a while, so I'm closing it per our new guidelines. Thank you for your contributions and please feel free to re-open in the future.

@flip1995 flip1995 closed this Dec 27, 2018
@flip1995 flip1995 added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Dec 27, 2018
flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Apr 15, 2020
Add lint for explicit deref and deref_mut method calls

This PR adds the lint `explicit_deref_method` that suggests replacing `deref()` and `deref_mut()` with `&*a` and `&mut *a`.

It doesn't lint inside macros.

This PR is the continuation of  rust-lang#3258.

changelog: Add lint `explicit_deref_method`.

Fixes: rust-lang#1566
@jonboh
Copy link
Contributor

jonboh commented Oct 10, 2023

@rustbot label -S-inactive-closed

@rustbot rustbot removed the S-inactive-closed Status: Closed due to inactivity label Oct 10, 2023
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.

Lint direct calls to deref or deref_mut
4 participants