The compiler is too sensitive

As a new one in Rust,even all new in study computer language,I think the compiler tell me "number = number - 1",should be "number -= 1" is too sensitive,

but thanks to the builders who is [virgo],I guess~I really can learn more.

1 Like

The compiler doesn't have a warning for replacing number = number - 1 with number -= 1. That's a Clippy lint in the style category.

5 Likes

Yeah, clippy has tons of extra lints to help enforce more correct code and more standard style. If you want, you can globally disable that lint in you crate by adding this to the top of your lib.rs file:

#![allow(clippy::assign_op_pattern)]

Or, if you would rather just not get any style suggests from clippy you can do:

#![allow(clippy::style)]
1 Like

I would be happy if everyone followed the advice of clippy out of the box and did not mess with it's setting.

That way we can create an ocean of software that is all written in the same style. Consistency removes all kind of little surprises when one is used to it. That would make reading each others code a smoother and more pleasant experience.

Similarly for rust fmt. A globally consistent formatting would be a joy. Rather than everyone doing their own thing with indenting, tabs, etc.

Yes, none of us are going to be happy about every detail of clippy and fmt. And feel the need to customize it for ourselves. I think we should be humble and resist the temptation. For the greater good.

5 Likes

To elaborate on the other answers, this is why it's in clippy and not the compiler. rustc itself is pretty restrained on what it will lint about, whereas clippy is highly opinionated since you have to choose to use it.

(That said, suggesting -= is absolutely something I'd want a quickfix for in an IDE, since I probably do want the shorter form.)

2 Likes

Every day the Rust compiler reprimands me for my transgressions a thousand times.

Even if the compiler shouted about the use of "a = a + 1" I don't think I would notice.

Which it does not.

2 Likes

Especially for rustfmt, short of mandating its use when compiling code e.g. by baking it into e.g. cargo build or rustc, this will never happen.

One reason I personally break away from default rust style is when writing macro_rule macros. Code in there can quickly become deeply nested and in such cases I care more about keeping the lines shorter than 80 chars so that I can still read it on a small laptop, than I care about adhering to some nice-in-principle-but-useless-in-context de facto standard.
In other words, I trade theoretical gain for real-world gain.

Others will have their own reasons for breaking from default rust style.

always use #![cfg_attr(feature = "cargo-clippy", allow(clippy::style))] at the begging of your project.
It avoids getting these useless warnings from clippy

For breaking out of the rustfmt style for short periods like in macros you can just add the #[rustfmt::skip] annotation.

I use that sometimes when formatting builders, or when putting matrices in code or something like that where lining things up a certain way helps readability.

You need like a meme from the opening of City of Ember: "For the good of all mankind." :upside_down_face:


I definitely agree there is great value in many cases to standardized style, but I also believe that it is right to allow people to change it. Saying that something should always be done is usually ( notice I didn't say always ) not true. There's usually edge cases and fully valid reasons for doing things differently.

1 Like