Static Analysis Tooling / Effective D

Artur Skawina via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 24 04:11:18 PDT 2014


On 04/24/14 04:56, Marco Leise via Digitalmars-d wrote:
> But »1 << size_t« doesn't always yield an int result! Compare to

It does. That expression *always* yields an int [1]. Assigning the
result to a wider type does not affect the value (the overflow has
already happened by then).

> size_t x = 1 << shiftAmount;

This would almost certainly be a bug, if shiftAmount could ever
become greater than 30. The `size_t = 1<<31` case is also very
unlikely to do what the programmer intended.

> - »auto« variable definition will resolve to »int« and may
>   lose information from expression »1 << shiftAmount«. Please
>   replace »auto« with »int« if that is what you want or set
>   the correct data type otherwise.

`size_t x = 1 << shiftAmount` is definitely not something that
should be recommended, see above. Just use the correct type on
the lhs of shift operators.

artur

[1] there could be an exception for certain compile-time contexts,
    but I don't think that would work in D (same code would give
    different results when run at CT and RT).


More information about the Digitalmars-d mailing list