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

Add a NOOP_METHOD_CALL lint for methods which should never be directly called #66

Closed
Aaron1011 opened this issue Oct 30, 2020 · 1 comment
Labels
meeting-proposal Proposal for a lang team design meeting T-lang

Comments

@Aaron1011
Copy link
Member

Based on rust-lang/compiler-team#375

Summary

Add a new lint NOOP_METHOD_CALL, which fires on calls to methods which are known to do nothing. To start with, we would lint calls to the following methods:

  • <&T as Clone>::clone
  • <&T as Borrow>::borrow
  • <&T as Deref>::deref
  • <&T as ToOwned>::to_owned

These trait impls are useful in generic code (e.g. you pass an &T to a function expecting a Clone argument), but are pointless when called directly (e.g. &bool::clone(&true)).

Note that we will intentionally not perform any kind of post-monomorphization checks. This lint will only fire on calls that are known to have the proper receiver (&T) at the call site (where the user could just remove the call).

For example

struct Foo;

fn clone_it<T: Clone>(val: T) -> T {
    val.clone() // No warning - we don't know if `T` is `&T`
}

fn main() {
	let val = &Foo;
	val.clone(); // WARNING: noop method call
	clone_it(val);
}

The precise mechanism used to indicate that these methods should be linted is not specified.
To start with, we could add an internal attribute #[noop], or hard-code a list of method paths in a lint visitor.

In the future, this could be made available to user code by stabilizing some mechanism to mark a function as being linted by NOOP_METHOD_CALL.
However, any such mechanism will need to have a way of dealing with blanekt impls (e.g. <&T as ToOwned>::to_owned goes through impl<T: Clone> ToOwned for T), which will require additional design work.

About this issue

This issue corresponds to a lang-team design meeting proposal. It corresponds
to a possible topic of discussion that may be scheduled for deeper discussion
during one of our design meetings.

@Aaron1011 Aaron1011 added T-lang meeting-proposal Proposal for a lang team design meeting labels Oct 30, 2020
@Aaron1011
Copy link
Member Author

Closing in favor of #67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meeting-proposal Proposal for a lang team design meeting T-lang
Projects
None yet
Development

No branches or pull requests

1 participant