Questions tagged [rust-clippy]

45 questions
55
votes
1 answer

How to disable a clippy lint for a single line / block?

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(self, mapper: F) ->…
Richard Rast
  • 1,772
  • 1
  • 14
  • 27
25
votes
2 answers

Can Rust's clippy do autocorrection / autofix?

Is it possible to run cargo clippy with an option so it will fix warnings automatically? From the help message, it does not look like this option is supported at the moment.
Sergey Potapov
  • 3,819
  • 3
  • 27
  • 46
14
votes
1 answer

How can I have a shared Clippy configuration for all the crates in a workspace?

I have an application split into several crates. I want to deny or allow a specific lint in all crates. For example: #![deny(clippy::print_stdout)] It seems I have to add this to lib.rs in each of the crates. There is an ticket with Cargo to allow…
dbr
  • 165,801
  • 69
  • 278
  • 343
13
votes
1 answer

How can I suppress a Clippy warning originating from a macro?

I have macro with return statement like this: macro_rules! return_fail { ( $res:expr ) => { match $res { Ok(val) => val, Err(e) => { eprintln!( "An error: on {}:{} {}; aborting…
너를 속였다
  • 899
  • 11
  • 26
11
votes
2 answers

Exclude dependencies when running Clippy

I'm trying to run clippy for the first time (I know.. I really should have done it by now eh?) and I am facing some errors. The project I'm trying to lint depends on Piston and it compiles and runs successfully. However, when I run clippy as…
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
9
votes
1 answer

Why does Clippy suggests passing an Arc as a reference?

I am checking Clippy findings in my code and found that the pedantic rule needless_pass_by_value might be a false positive. It says that: warning: this argument is passed by value, but not consumed in the function body help: consider taking a…
Akiner Alkan
  • 6,145
  • 3
  • 32
  • 68
8
votes
1 answer

Dead code warning with multiple binaries?

I noticed that in order for a piece of code to not be classified as dead, it has to be reachable from all binaries. Example: Cargo.toml: [[bin]] name = "main_one" path = "src/main_one.rs" [[bin]] name = "main_two" path =…
Vilda
  • 1,675
  • 1
  • 20
  • 50
8
votes
1 answer

How to stop `cargo clippy` from running on dependencies as well?

I have a workspace with many crates. I want to run cargo clippy only one of of them them and not any of its dependencies. How do I accomplish this?
user855
  • 19,048
  • 38
  • 98
  • 162
5
votes
1 answer

How do I fix Clippy's needless_range_loop for loops that copy between slices with an offset?

When running cargo clippy, it complains about code like this: pub fn from_bytes(data: [u8; 72]) -> Stuff { let mut ts = [0u8; 8]; let mut cs = [0u8; 64]; for b in 0..8 { ts[b] = data[b]; } for bb in 0..64 { …
jan
  • 211
  • 1
  • 7
4
votes
1 answer

Why are some clippy lints gone if my modules are public?

I have a code that looks approximately like this: // src/bitboard.rs #[derive(Copy, Clone, Debug)] pub struct Bitboard { value: u64 } impl Bitboard { pub const fn new(value: u64) -> Self { Self { value } } pub const fn…
Solens
  • 93
  • 2
  • 6
4
votes
1 answer

Clippy says `too many arguments` to static declaration

This code is from my OS. #[global_allocator] pub static ALLOCATOR: LockedHeap = LockedHeap::empty(); Clippy says this function has too many arguments. error: this function has too many arguments (4/3) --> src/mem/allocator/heap.rs:14:1 | 14 |…
toku-sa-n
  • 798
  • 1
  • 8
  • 27
3
votes
1 answer

rust how to collapse if let - clippy suggestion

I run cargo clippy to get some feedback on my code and clippy told me that I can somehow collapse a if let. Here is the exact "warning": warning: this `if let` can be collapsed into the outer `if let` --> src\main.rs:107:21 | 107 | / …
Teiem
  • 1,329
  • 15
  • 32
3
votes
1 answer

Rust Range.contains failed to be inlined/optimized

I was running my code through Clippy and it suggested changing the following: const SPECIAL_VALUE: u8 = 0; // May change eventually. pub fn version1(value: u8) -> bool { (value >= 1 && value <= 9) || value == SPECIAL_VALUE } Into pub fn…
senevoldsen
  • 337
  • 3
  • 8
3
votes
0 answers

`cargo clippy` only produces output on the first run

I wanted to use Clippy to lint my code using Rust 1.31.1. I added a obvious mistake for the linker (and surely there is more to lint): if (nr_children == 0) == true {... I installed Clippy via rustup: rustup component add clippy (and clippy-preview…
Jounathaen
  • 803
  • 1
  • 9
  • 23
2
votes
1 answer

How to prevent cargo clippy from analyizing generated prost files

I am using prost to generate rust classes for protobufs. I want clippy to ignore these generated files and I'm having trouble figuring out how to make clippy ignore them. In my lib.rs file, I have pub mod modes { #[allow(clippy)] …
Connor
  • 545
  • 4
  • 20
1
2 3