55

I am getting some Clippy lints that look like this:

warning: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
  --> src/helpers/mod.rs:29:32
   |
29 |     pub fn to_vec_sorted<U, F>(self, mapper: F) -> Vec<U>
   |                                ^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention

I have no problem dealing with this lint, I just picked it because it doesn't show any proprietary code. Suppose I had a really good reason why I needed to name the function this way, and also that Clippy is integrated into my CI, so I need to have zero Clippy errors / warnings.

Is there a way to disable a Clippy lint for a particular line or code block, analogous to @SuppressWarnings("whatever") in Java? I feel like there must be, but I can't find any examples of doing this in the documentation.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Richard Rast
  • 1,772
  • 1
  • 14
  • 27
  • https://github.com/rust-lang/rust-clippy#allowingdenying-lints – zrzka Mar 28 '19 at 16:49
  • Without context it's hard to say for sure, but probably the best way to get rid of the warning, is to fix the code and rename the method to `into_vec_sorted`. – mcarton Mar 28 '19 at 17:36
  • 4
    @mcarton If you read the prose it points out that this lint is easy to solve, it's just present to give a minimal example. I'm looking for a general way to disable a lint in a narrow scope, which was provided in an answer below. – Richard Rast Mar 28 '19 at 17:43

1 Answers1

68

The docs state you can allow or deny lints.

#[allow(clippy::wrong_self_convention)] pub fn to_vec_sorted<U, F>(self, mapper: F) -> Vec<U>

And ,if you want to disable all 1 of them:

#[allow(clippy::all)] pub fn to_vec_sorted<U, F>(self, mapper: F) -> Vec<U>

1: clippy:all doesn't actually allow all lints, rather everything contained by correctness, suspicious, style, complexity, cargo, and perf. This means no pedantic or nursery lints..
CATboardBETA
  • 418
  • 6
  • 29
Ealhad
  • 1,912
  • 20
  • 27