3 ms·
While Rust won't catch the use of subtraction in an arbitrary function named "add", it'll catch (in clippy) the use of subtraction in an implementation of the A
by JoshTriplett 5y ago
While Rust won't catch the use of subtraction in an arbitrary function named "add", it'll catch (in clippy) the use of subtraction in an implementation of the Add or AddAssign traits: https://rust-lang.github.io/rust-clippy/master/#suspicious_arithmetic_impl https://rust-lang.github.io/rust-clippy/master/#suspicious_a...
- majewsky 5y agoThis is one of those lints where there's probably an entire backstory to it.
- cesarb 5y agoProbably not much of a backstory, it's common to make copy-and-paste mistakes when implementing multiple similar things. For instance, when implementing both the Add and Sub traits, it would be normal to start with one of them, and duplicate it to make the other, making the relevant changes in the copy (Add -> Sub, add -> sub, + -> -); it's easy to forget to make one of these changes.
- nsajko 5y agoThis sounds horrid.
- bitcharmer 5y agoWhy?
- scoutt 5y agoNot OP, but what happens in the case you want to add by subtracting a negative value?
- gpm 5y agoYou disable the warning for the function?
- dureuill 5y agoYes, which also serves as a way to explicitly point out that the "surprising operation" is intended. Hopefully this even reminds you of putting an explanatory comment there.
- doteka 5y agoKnowing clippy I wouldn’t be surprised if that compiled, but resulted in a warning that you can simplify the double negative.
- vurpo 5y agoClippy is a linter. You don't use linters by mechanically following its warnings as gospel, you use it by double-checking the code wherever it warns and removing the warning from that function if it's a false positive.
- gameswithgo 5y agoclippy is an optional linting tool, don't have to use it, and if you do use it you can configure it to not care about this