Bug 45358 - Diagnostic could be issued for old C style a =+ b and similar cases
Summary: Diagnostic could be issued for old C style a =+ b and similar cases
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.4.3
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 106148 (view as bug list)
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2010-08-20 15:10 UTC by Christian Leber
Modified: 2022-07-01 00:13 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-08-20 19:46:42


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christian Leber 2010-08-20 15:10:51 UTC
Hello,

there is a minor issue when =+ is used by accident instead of +=.

It is unfortunate there there is not even a warning, because this is a typo that can cost lots and lots of time.
Furthermore it seems that =+ is was the old syntax in the old pre ANSI C.

=+ behaves like =
(mathematically it is of course the same, but the behavior is still wrong,
but there is also never a reason to write =+ instead of =)

#include <stdio.h>
int main(void) {
        int a=1,b=1;

        a += 2;
        b =+ 2;
        printf("a=%d b=%d\n",a,b);
        return 0;
}

(no warning, not even with -Wall)
a=3 b=2

It would be nice if future version could at least throw a warning.

Regards
Christian Leber
Comment 1 Jakub Jelinek 2010-08-20 15:23:53 UTC
=+ in x =+ y; isn't one token, but two, it is the same as if you write
x = + y ;
And, unary + is standard C unary operator:
The result of the unary + operator is the value of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.

So, I don't think there is anything that should be warned about, it is normal valid C.
Comment 2 Andreas Schwab 2010-08-20 15:35:52 UTC
There is a lot of normal valid C we warn about...
Comment 3 Jonathan Wakely 2010-08-20 15:36:18 UTC
Yes, "if (b = 2)" is valid and -Wparentheses warns about that.

(In reply to comment #0)
> It would be nice if future version could at least throw a warning.

Obviously it can't be anything *more* than a warning.

N.B. at least in C++ there are valid reasons to use the + operator, such as turning an lvalue into an rvalue.
Comment 4 Jakub Jelinek 2010-08-20 16:02:04 UTC
I think we certainly don't want to warn for = +, or =/**/+, so if anything, there could be a warning for = token immediately followed by a token that makes a valid <op>= token (i.e. the same file, same line, column 1 above the = column).
Comment 5 Tom de Vries 2019-01-31 12:30:23 UTC
Cross-referencing PR89136 - "libbacktrace/elf.c:2941: suspicious assignment"
Comment 6 Eric Gallager 2019-05-01 09:00:43 UTC
making this block the "new-warning" meta-bug
Comment 7 Eric Gallager 2022-07-01 00:13:09 UTC
*** Bug 106148 has been marked as a duplicate of this bug. ***