←back to thread

611 points LorenDB | 1 comments | | HN request time: 0s | source
Show context
grumbel ◴[] No.43908210[source]
There is '-Wconversion' to catch things like this. It will however not trigger in this specific case since g++ assumes converting 1000.0 to 1000 is ok due to no loss in precision.

Quantity(100) is counterproductive here, as that doesn't narrow the type, it does the opposite, it casts whatever value is given to the type, so even Quantity(100.5) will still work, while just plain 100.5 would have given an error with '-Wconversion'.

replies(2): >>43908438 #>>43908555 #
b5n ◴[] No.43908555[source]
> -Wconversion ... assumes converting 1000.0 to 1000 is ok due to no loss in precision.

Additionally, `clang-tidy` catches this via `bugprone-narrowing-conversions` and your linter will alert if properly configured.

replies(1): >>43908717 #
kelnos ◴[] No.43908717[source]
My opinion is that if you need to run extra tools/linters in order to catch basic errors, the language & its compiler are not doing enough to protect me from correctness bugs.

I do run clippy on my Rust projects, but that's a matter of style and readability, not correctness (for the most part!).

replies(5): >>43908776 #>>43909387 #>>43909947 #>>43912620 #>>43912990 #
1. jpc0 ◴[] No.43908776[source]
How much of what Rust the language checks is actually linter checks implemented in the compiler?

Conversions may be fine and even useful in many cases, in this case it isn’t. Converting to std::variant or std::optional are some of those cases that are really nice.