rust-lang / lang-team

Home of the Rust lang team
http://lang-team.rust-lang.org/
Apache License 2.0
202 stars 48 forks source link

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

Closed Aaron1011 closed 3 years ago

Aaron1011 commented 3 years ago

Based on https://github.com/rust-lang/compiler-team/issues/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:

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 commented 3 years ago

Closing in favor of https://github.com/rust-lang/lang-team/issues/67