rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Better documentation for disallowed_method lint

Open juliancoffee opened this issue 4 years ago • 6 comments

https://rust-lang.github.io/rust-clippy/master/#disallowed_method Doc on All Clippy Lints shows the example of how to use it with methods from the standard library, but no example on how to use it with user-defined types neither if this is possible to use with methods of user-defined types/imported items.

When I tried to use this with nightly-2021-07-06 in rust-toolchain I managed to get it to work with std::vec::Vec::append, but no luck with user-defined methods.

juliancoffee avatar Jul 22 '21 19:07 juliancoffee

Saw this commit https://github.com/rust-lang/rust-clippy/commit/70ce0c2c55b18f7c9a4bc8d767c626f8d5948fae if I understand correctly, this is worked on currently?

juliancoffee avatar Jul 24 '21 00:07 juliancoffee

I believe local methods still can't be used with this lint.

For example, in a crate called my_crate with the following contents:

fn f() { }

fn g() {
    f();
}

And with the following clippy.toml

disallowed-methods = [
  "f"
]

The call in g isn't warned about.

Using crate::f, my_crate::f and ::my_crate::f also doesn't work.

Zenithsiz avatar Feb 02 '22 20:02 Zenithsiz

As the clippy.toml can be defined for workspace, I suppose my_crate::f should work as a design (but it doesn't work yet).

tisonkun avatar Aug 04 '23 01:08 tisonkun

Besides, I may want to disallow trait method for a specific implementation. Given -

trait A { fn f (); }
Struct B; impl A for B { ... }
Struct C; impl A for C { ... }

I'd like to forbit B.f() but not C.f(). It seems now we all resolved to A::f().

tisonkun avatar Aug 04 '23 02:08 tisonkun

my_crate::f

I checked this way works now (may be related to https://github.com/rust-lang/rust-clippy/pull/8852) and even with nested mod. My issue is the comment above https://github.com/rust-lang/rust-clippy/issues/7479#issuecomment-1664869201

tisonkun avatar Aug 04 '23 02:08 tisonkun

Note: Today (Rust 1.81, though it's probably worked for a while), this works in clippy.toml:

[[disallowed-methods]]
path = "std::fs::rename"
reason = "Use fs_err::rename"

However, the documentation only includes examples for methods on types, not methods on modules, so we should clean that up.

9999years avatar Oct 16 '24 17:10 9999years