top | item 20201247

(no title)

NotPaidToPost | 6 years ago

I don't think it would not be simple or a good idea to warn because these are perfectly valid expressions and it would lead to a flood of warnings if compilers started to display "Did you really mean that?" for an ever growing list of expressions (which is what it would become).

Better to leave this to code analysis tools.

Edit:

2^32 uses the ^ operator exactly as intended, and has perfectly legitimate uses, especially when dealing with registers of HW peripherals.

It's not the same as warning on "if (a=b)" as someone replied.

From a language perspective, 2^32 is exactly the same as 2+32.

discuss

order

rocqua|6 years ago

It's a warning not an error. Moreover, it is very hard to come up with a legitimate usecase of 2^32 as an expression. People who use it to set bit flags will use &, and for bit flags, octal or hex notation is probably better.

raxxorrax|6 years ago

I don't know if it is a good practice, but I often have statements and expressions that look awfully similar. Flipping bits does happen often enough.

Mostly written as y ^ (1 << x), which could easily resolve to these expressions. Mostly at runtime, sure, but there are exceptions. Especially if you like descriptive constants. I would expect it to be quite difficult to separate the "correct cases" without people starting to just suppress compiler warnings or trick it with writing the same stuff in different words (which might be better).

On the other hand, setting max_short to something like 18 is probably a really god prank in larger programming environments. The compiler would just ruin all the fun here.

pmikesell|6 years ago

Yes, or 1 << 31, for example.

NotPaidToPost|6 years ago

> It's a warning not an error.

Many build environments are set to treat all warnings as errors.

The point is that 2^32 is a perfectly compliant C expression that is neither misleading nor ambiguous, and that also won't create any variable overflow. It uses ^ exactly as intended. Why should the compiler complain? Why should I get a warning/error when following the spec to the letter?

masklinn|6 years ago

> It's not the same as warning on "if (a=b)" as someone replied.

"if (a=b)" uses the "=" operator exactly as intended with perfectly legitimate uses. It's warned about because it's an error-prone construct, not because it's incorrect.

Seems to me 2^32 is also error-prone, if you wanted to combine flags you'd use 2|32, using xor here is weird.

NotPaidToPost|6 years ago

'If' expects a condition so finding an assignment may be a red flag even if an assignment has a return value in the spec, which makes the construct valid.

On the other hand ^ expects integers so 2^32 is exactly what is expected.

Most the replies I saw here try to second-guess or claim that ^ should be used in a specific way. Not so, ^ is just doing XOR of two integers.

Apparently, I am having an incorrect opinion, though, so I will self-censor and remain silent.

blattimwind|6 years ago

> I don't think it would not be simple or a good idea to warn because these are perfectly valid expressions and it would lead to a flood of warnings if compilers started to display "Did you really mean that?" for an ever growing list of expressions (which is what it would become).

There are many technically valid expressions and statements that will still generate warnings in most compilers.

if(a=b) {} comes to mind first and foremost.

estebank|6 years ago

I'm intrigued at what your opinion is about the following Rust errors:

    error[E0308]: mismatched types
     --> src/main.rs:4:8
      |
    4 |     if x = y { println!("eq") }
      |        ^^^^^
      |        |
      |        expected bool, found ()
      |        help: try comparing for equality: `x == y`
      |
      = note: expected type `bool`
                 found type `()`

    error: `<` is interpreted as a start of generic arguments for `u32`, not a comparison
     --> src/main.rs:4:17
      |
    4 |     if i as u32 < 2 {
      |        -------- ^ --- interpreted as generic arguments
      |        |        |
      |        |        not interpreted as comparison
      |        help: try comparing the cast value: `(i as u32)`

__david__|6 years ago

For the same reason that this:

    if (c)
        printf("Hello");
        exit(0);
    exit(1);
Produces a warning in gcc:

    test.c:7:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
         if (c)
         ^~
    test.c:9:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
             exit(0);
             ^~~~
"2^32" is of course just an xor, but it makes no sense (the "exclusive" part of it is not being exercised). If you really mean 2 bits being set in a constant then "2 | 32" is much clearer. If you're dead set on xor, "32^2" won't trigger the warning.

NotPaidToPost|6 years ago

It's not for you or the compiler to decide that 2^32 makes no sense. It is using ^ exactly as intended and is neither misleading nor ambiguous.

Stating that 32^2 should not trigger a warning while 2^32 should shows that this proposal has not been thought through.

It's not desirable or sensible to raise a warning on the premise that the expression might mean something else in another language, which is what this would do.