arithmetic_side_effects doesn't fire in non-const function located inside of const definition #13845
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-negative
Issue: The lint should have been triggered on code, but wasn't
Summary
arithmetic_side_effects
doesn't fire in non-const function located inside of const definition. (See below for why it is important for me.)Lint Name
arithmetic_side_effects
Reproducer
I tried this code:
I expected to see this happen:
arithmetic_side_effects
should fire.Instead, this happened: nothing fired.
Note: it is documented that
arithmetic_side_effects
will not fire in const contexts. But this is not const context.Okay, now let me describe, why this is important and how this actually breaks my workflow.
Okay, so I use crate
alloy
for managing my money.alloy
provides typeU256
for dealing with monetary values.U256
is borrowed from another crateruint
. I don't want silent overflows, I want panics on overflow, so I filled bug toruint
, hoping they will add panics-on-overflow: recmo/uint#408 . But a developer said that they don't want to change the behavior, so I have to resort to some hacks on my side. So I decided to use clippy's lintarithmetic_side_effects
to eliminate arithmetic operations withU256
in my code and usechecked_add
and similar instead.But I'm not sure that
arithmetic_side_effects
will always work. It is possible that it will change its behavior in some next version of clippy. Notably,arithmetic_side_effects
currently doesn't fire onWrapping
. It is possible that following same logic authors of clippy will disable this lint forU256
, too, becauseU256
has wrapping behavior, too! So, I need some way to be sure thatarithmetic_side_effects
will always fire in the future. So, I decided to use#[expect]
.So, I put this function to my code:
Okay, this works, but has some problems. First,
f
is not used, so it fires warning about dead code. Second, I need to invent new name for every such function, despite I don't plan to call them. And finally, such functions pollute global name space.It is possible to put this function to some module, but then the problem reappears again, because now I have to invent some name for this module, despite I don't plan to use it.
So, I decided to use
const _: ()
trick:But this is const context. And
arithmetic_side_effects
is documented not to fire in the const contexts. Moreover, this code doesn't even compile. So, I did this:Unfortunately, this code gives message
this lint expectation is unfulfilled
. It seems this non-const context is treated as const by clippy. This is a bug, and thus I reported itVersion
The text was updated successfully, but these errors were encountered: