4

Given the following code:

bool doGoodThing;

switch (some_variable)
{
case 1:
case 2:
    doGoodThing = true;

default:
    doGoodThing = false;
}

Latest gcc version are smart enough to detect when variables are being assigned to but still not used, etcetera. Is there any compiler or flag that could trigger a warning saying that the whole switch does not make any sense for a code like this?

UPDATE: The question is not about doGoodThing variable. It is about a silly switch statement that doesn't make much sense.

UPDATE 2: Passing "dupe" voters, before you mark it as a duplicate - read a question one more time. This is not about a warning for a missing "break" statement after "case". This is about dead code, logical errors, static semantics analysis of the code by compiler. I do not need a warning if "break" is not there.

  • This snippet makes perfectly sense to me. – BlackBear Dec 16 '11 at 15:45
  • http://stackoverflow.com/questions/7703358/how-can-i-tell-gcc-to-warn-or-fail-on-switch-case-statements-without-a-break – cnicutar Dec 16 '11 at 15:47
  • In what sense it doesn't make sense? Do you want the compiler to check the semantic meaning of your code? – Nawaz Dec 16 '11 at 15:48
  • 4
    @BlackBear The code would behave the exact same way if the `doGoodThing = true` line were removed, so there's either an unnecessary line of code or a bug. It seems perfectly reasonable to me for the compiler to warn about something like that. – sepp2k Dec 16 '11 at 15:48
  • @Nawaz: Yes. And it does it for many other things. I am curious if this is possible to do for cases like this. –  Dec 16 '11 at 15:50
  • @VladLazarenko "I am curious if this is possible to do for cases like this." Pun intended? – Joe Dec 16 '11 at 16:08
  • @Caleb: He wants to be warned about the fact that the assignment `doGoodThing = true;` won't have any effect no matter what happens. – sepp2k Dec 16 '11 at 16:18
  • @closers: it is not the same question as 7703358. In 7703358 the issue was: warn me if a case fallstru. Here it is: warn me if there is **no difference between any of the cases**. – wildplasser Dec 16 '11 at 16:42
  • 1
    @sepp2k Thanks for the clarification. Considering that many people apparently misunderstood the OP's intent, it's clear that the question should be rewritten to state the actual question. @Vlad, tell us what the question *is*, not what the question *isn't*. I don't understand why the question even bothers with the `switch` -- it sounds like your real question is about ineffective changes to variables. For example, should `foo = 1; foo = 2;` trigger the warning? The first statement is obviously unnecessary. How about `if (1 == 2) foo(); else bar();`? – Caleb Dec 16 '11 at 16:45
  • @Caleb: Please suggest an edit that makes it clear. Switch case is a practical example of a bug that did cost thousands of dollars. If it was `1 == 2`, I'd ask about that. My personal opinion is that should trigger a warning, too. –  Dec 16 '11 at 16:50
  • @VladLazarenko The switch seems to confuse more than help, because an obvious solution to the example problem is to insert a break after case 2. You seem to be saying that you don't want the break, but you want the compiler to warn that the first of two consecutive assignments is unnecessary, and that the whole expression could be simplified? Where do you draw the line? It sounds like you'd want a warning whenever an expression could be simplified. – Caleb Dec 16 '11 at 17:07
  • The real-world cases for this kind of thing are 1) The preprocessor might have suppressed a few cases and/or breaks. 2) the dreaded "volatile" keyword might force the variable to actually be written twice. Point is: there are perfectly valid scenarios for this construct to exist, and there are cases where you want to be warned. The Question is perfectly valid and differs sufficiently from 7703358. I rest my case. – wildplasser Dec 16 '11 at 18:14
  • @wildplasser: It could be intentional, but I think that either suppressing a warning using pragma or marking as volatile must be used. Otherwise this could lead to dramatic consequences :-) –  Dec 16 '11 at 18:17
  • My downvote is to emphasize Caleb's comment above: the question is not specified in the question. Fix that and I'll flip it. – dmckee --- ex-moderator kitten Dec 16 '11 at 22:54

2 Answers2

5

If you're talking about the missing break in the switch statement, it is an asked ehancement in GCC.

EDIT 2: if you were using Java, FindBugs could do it for you

EDIT again : CPPCheck seems to detect something related :

redundant assignment in a switch statement

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • (+1) Was about to post the same link. – NPE Dec 16 '11 at 15:51
  • This is close, but I use switch cases without breaks all over the code. If gcc starts to give me false positives, I'd just disable that warning. It would be nice to detect possible bugs or dead code and warn only about those cases. But a good answer, thanks. –  Dec 16 '11 at 15:53
  • @Als: That question is not a duplicate, but related. I do not want to warn about missing break. I need semantics analyzer to go deeper. –  Dec 16 '11 at 15:54
  • 1
    @VladLazarenko: Then you are looking for code analysis tools really not compiler feature. – Alok Save Dec 16 '11 at 15:55
  • 2
    @Als: How would warning about something like this out of scope for a compiler when warning about e.g. unused variables or execution paths without a return statement is in scope? It seems to me that those things require the same amount of static analysis. – sepp2k Dec 16 '11 at 15:57
  • @Als: Disagreed. This is in fact a long time compiler feature. Take a look at what gcc can do. –  Dec 16 '11 at 15:57
  • Since when does Valgrind do static analysis? Valgrind actually runs the code it checks (and it doesn't warn about any code that isn't executed during that particular run). Also Valgrind wouldn't warn about this. – sepp2k Dec 16 '11 at 16:13
4

Okay, so we're looking for a rule by which

bool doGoodThing;

switch (some_variable)
{
case 1:
case 2:
    doGoodThing = true;

default:
    doGoodThing = false;
}

would produce a warning, but

bool doGoodThing;

switch (some_variable)
{
case 1:
case 2:
    doGoodThing = true;
    break;

default:
    doGoodThing = false;
}

(which presumably is the intended code here) would not. One relatively simple way to go about this would be to always warn about fall-through except if a case is empty. I.e. don't warn about the fall through from case 1 to case 2 because there's no code in between, but warn about the fall through from 2 to default. However this approach would still warn about code that might be intentional.

A more sophisticated rule would be this: Produce a warning whenever there is an assignment to a variable x such that there is no possible execution path, such that the assigned value would be used. I.e. in all possible execution paths that involve x being assigned the value v, x will either be reassigned to something else or go out of scope before any code that uses x is executed. This would warn about your code, but not the fixed example.

Performing this analysis is definitely possible. However I'm not aware of any compiler that currently does such analysis (though all that really means is that gcc doesn't).

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 2
    Hooray, someone who apperently understood the question (or I misunderstood it too). – Xeo Dec 16 '11 at 16:12
  • 2
    "Produce a warning whenever there is an assignment to a variable x such that there is no possible execution path, such that the assigned value would be used." - This! Seems like a pretty obvious thing to detect and warn about, and gcc does already have some warnings about unused values. I think the switch statement is a bit of a red herring here since it's irrelevant to the analysis whether this problem is caused by a switch statement or some other logically equivalent thing that's likewise probably wrong. For example `if (condition) foo = 1; foo = 2;` – Steve Jessop Dec 16 '11 at 17:38