#Clippy lints

79 messages · Page 1 of 1 (latest)

hollow venture
#

Which clippy lints do you like to enable when creating a new project?

full pelican
#

pedantic is a good set that isn't too bad

#

(i.e. clippy::pedantic)

#

also don't forget about the builtin lints

#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(noop_method_call)]
#![warn(trivial_casts)]
#![warn(trivial_numeric_casts)]
#![warn(unreachable_pub)]
#![warn(unused_import_braces)]
#![warn(unused_lifetimes)]
#![warn(unused_qualifications)]
lapis seal
#

not a clippy lint, but be sure to enable #![warn(rust_2018_idioms)], which sadly isn't warn-by-default currently

rancid cape
#

I like the builtin lint deny(unsafe_op_in_unsafe_fn)

quiet trout
#
#![deny(rust_2018_idioms)]
#![warn(clippy::cast_lossless)]
#![warn(clippy::exhaustive_enums)]
#![warn(clippy::exhaustive_structs)]
rancid cape
#

I like having to actually scope unsafe operations, and also unsafe in a fn signature should be about the api and unsafe {} blocks should be the operation

quiet trout
elder flicker
#

as well as clippy::pedantic I also use clippy::wrong_pub_self_convention

#

wait, that's not a thing anymore?

#

right yeah, seems it was merged into clippy::wrong_self_convention, nice

simple marlin
lapis seal
#

it's things for the 2018 edition
like things that were added/deprecated there compared to 2015

simple marlin
#

Cool. Is there one available for 2021?

quiet trout
#

2021 didn't deprecate any language features in the same sort of way

hard heart
#

Are there some clippy lints people turn off? Like I don't like the ones that insist you use char literal instead of a str literal. That's just annoying and pointless.

quiet trout
#

not counting case-by-case,

#![allow(clippy::collapsible_if)]
#![allow(clippy::collapsible_else_if)]
#![cfg_attr(test,
    allow(clippy::float_cmp), // Tests work with predictable floats
    allow(clippy::redundant_clone), // Tests prefer regularity over efficiency
)]
hard heart
#

Oooh, allowing lints specifically for all tests make a lot of sense.

quiet trout
#

I don't like collapsing ifs when the algorithm I'm trying to express is very specifically "check for A, and only if that's true, compute B, and if that did something, …"

rancid cape
#

yeah encoding logic in short circuiting can sometimes be tricky

quiet trout
#

also in the case of

if case_a_not_b {
    // Case A
    handle_case_a();
} else {
    // Case B
    if condition_in_case_b() {
        handle_case_b_nontrivial();
    }
}
#

I prefer a more symmetric if-else over making that an if-else-if, but collapsible_else_if doesn't like that style

quiet trout
tardy wharf
hard heart
simple marlin
#

Do I have to enable this lints in the code? Or can I throw them in a file like clippy.toml?

rancid pumice
#

Wow, I added

#![warn(
    clippy::all,
    clippy::restriction,
    clippy::pedantic,
    clippy::nursery,
    clippy::cargo
)]

to the beginning of a project and clippy generated 600 warnings.

lapis seal
#

and how many of them were useful?

elder flicker
#

Including the warning that tells you not to #![warn(clippy::restriction)] I'm assuming

rancid pumice
#

most of them were about using non ASCII literals

#

and changing them to utf byte code or soemthing

#

not a chance thats an improvement

elder flicker
#

The restriction category also warns against not using return at the end of function bodies

#

it's not really meant to be used

rancid pumice
#

yeah, I didn't know restriction was so extreme

#

it is sort of just for people who don't like rust idioms I guess

elder flicker
#

pretty much

full pelican
#

like
the one about not using integer arithmetic

#

is a pretty useful one

#

in specific circumstances

rancid pumice
full pelican
#

?clippy

#![forbid(clippy::integer_arithmetic)]

fn _foo() {
  let x = 2;
  let y = 255;
  let _z = x + y;
}
jade lichenBOT
#
error: integer arithmetic detected
 --> src/main.rs:6:12
  |
6 |   let _z = x + y;
  |            ^^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:11
  |
1 | #![forbid(clippy::integer_arithmetic)]
  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic

error: could not compile `playground` due to previous error
rancid pumice
#

using fallible functions?

full pelican
#

it's about integer overflow

rancid pumice
#

I see

full pelican
#

i used it recently to basically go "okay, show me all the places i can overflow"

#

and then i manually went through and inspected all of them

rancid pumice
#

the folks at linux would appreciate that if they ever pick up rust.

full pelican
#

and went "okay this overflow can't happen, this one can't happen, this one can"

#

it's not really to be used globally

rancid pumice
#

yeah, that can be extremely useful

full pelican
#

but if you've been having problems with integer overflow

#

getting the compiler to output a nice list of every thing that might be a source of an overflow

#

is useful

simple marlin
#

Is the overflow possibility because rust picks the lowest size for each literal while doing type inference?

#

Does it happen if you do let x : u32 = 2;?

full pelican
#

well, no, there's no overflow here

#

it's just saying there might be

lapis seal
#

running clippy::pedantic on my project

#

getting tons and tons of docs for function returning Result missing # Errors section for internal functions
ferrisBut

#

they are really pedantic

#

binding's name is too similar to existing binding
didn't know it did that

#
warning: unnecessary structure name repetition
  --> amqp_transport/src/methods/mod.rs:46:23
   |
46 |         let mut vec = Vec::with_capacity(len);
   |                       ^^^ help: use the applicable keyword: `Self`
   |

I can see why this is nursery

#

i just happen to not be alloc

#

and not implementing Vec

#

but understandable that clippy would think I am

lapis seal
#
warning: item name ends with its containing module's name
 --> amqp_transport/src/error.rs:5:42
  |
5 | pub use amqp_core::error::{ConException, ProtocolError};
  |                                          ^^^^^^^^^^^^^
  |
  = note: `-W clippy::module-name-repetitions` implied by `-W clippy::pedantic`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#module_name_repetitions

wow clippy, thank you

simple marlin
#

Hehe, kinda like that one tho. With Error being an obvious exception

small coral
#

protocol::Error > ProtocolError

simple marlin
#

O_o

shrewd hearth
rancid pumice
#

you know, I almost never index arrays

#

i tend to abuse iterators in this case.