15

We have a project using CDT in Eclipse. It's an old project that we just imported into Eclipse, and I want to ensure we start using static code analysis to find any weirdnesses.

The thing is, there are a bunch of lines that trigger warnings that we want to just ignore, with the main ones being fallthroughs within switch statements.

I know how to do this for lint, but what about for CDT? Is there a single-line comment that I can put right above the line?

Example: ("No break at the end of case")

  case enChA:  
    nChannel++;
    // I want to ignore this fallthrough       
  case enChB:      
    nChannel++;
    // And this one...
  case enChC:
    nChannel++;
    // And this one...
  case enChD:
    nChannel++;
    // do some more stuff...
    break;
c.fogelklou
  • 1,781
  • 1
  • 13
  • 26
  • This question would attract more interest with a more descriptive title that refers to break at the end of a case in a switch statement. – Craig McQueen Feb 27 '18 at 23:09
  • @CraigMcQueen, Thanks for the tip, but I was actually asking about how to turn off static code analysis on a line by line basis. The break was just a specific example of a warning that I wanted to turn off. Everyone focused on the example, but that wasn't actually the original question. – c.fogelklou Mar 01 '18 at 06:04

6 Answers6

27

You should try

//no break

before the next case.

  • I accepted your answer, but I also would like to ignore some "unused variable declarations." Any tips on doing that? – c.fogelklou Jun 05 '13 at 10:27
  • 1
    Thanks. Works also as `/* no break */` or `/* no break at the end of case */`. (I can't use `//` comments because I'm limited to C89.) – Martin Scharrer Feb 17 '15 at 10:24
  • 2
    I sure would be curious to know just why the numb nuts at eclipse decided "no break" is better than the *30* year old "FALLTHROUGH". What were they thinking? – Bruce K May 27 '15 at 20:42
  • 1
    @BruceK: In Eclipse you can personalize the message. The default value is `no break` but I changed it now to `FALLTHROUGH` and it works fine! – Peter VARGA Jul 02 '15 at 21:27
  • 1
    @AlBundy Thank you. I had learned that already. The question has to do with "what were they thinking?" They effectively threw a wrench into a 30 year old lint tradition for no discernible reason. Yes, I can hack around it with enough research. Research we both and countless others were forced to do. Needlessly. – Bruce K Jul 04 '15 at 15:25
11

These settings are located under Window -> Preferences -> C/C++ -> Code Analysis. You can customize the settings. For example if you pick No break at the end of case, you can define the comment that suppresses the warning. By default it's "no break". So coincidentally copy/pasting the warning message into the comment worked in your case:

CDT static code analysis

As you can see the text doesn't have to be an exact match and it doesn't seem to be case sensitive either.

Referring to your follow-up question about unused variables: When you customize Unused variable in file scope you can define variable names that should be ignored:

enter image description here There are two cryptic predefined exceptions "@(#)" and "$Id". Unfortunately I couldn't find any official documentation so I went looking into the source. It looks like the checker simply tests if a variable name contains() any of the specified exceptions. If it does, the warning is suppressed.

Outside of Eclipse CDT, there's the popular void-casting trick. If the compiler warns about an unused variable, cast it to void. This operation has no effect, so it's safe, but from the perspective of the compiler that variable is now used. I usually wrap it in a macro to make abundantly clear what I'm doing, e.g.

#define UNUSED(var) (void)(var)

void foobar()
{
    int b;     // not used. 
    UNUSED(b); // now it's used
}
Community
  • 1
  • 1
djf
  • 6,592
  • 6
  • 44
  • 62
3

Solved it.

I just added the text from the warning that I wanted to ignore to immediately above where the break would be.

Like this:

      case enChC:
        ++nChannel;
        //No break at the end of case
      case enChD:
        ++nChannel;
johnchen902
  • 9,531
  • 1
  • 27
  • 69
c.fogelklou
  • 1,781
  • 1
  • 13
  • 26
  • 2
    I came here with the same problem. I never would have found this setting without SE. By default in Eclipse, if you have the exact string "no break" anywhere in a comment where a break would normally go (right before the next case statement), it will remove the warning. It's not case sensitive and even "Give me a rhino breaker" works! LOL – dslake Mar 19 '16 at 05:00
2

As is has been said, in this specific case, it can be solved adding the comment:

//no break

or:

//no break at the end of case

What really matters is the (no break).

But also, it is required that you don't have more comments between the end of this case and the next one or it won't work. For example the next case will still result in a warning:

case enChC:
    ++nChannel;
    //No break
    //This second case decrease the value
  case enChD:
    ++nChannel;
Omsitelta
  • 105
  • 9
0

UPDATE 2021

  • Version: 2021-03 (4.19.0)
  • Build id: 20210312-0638

Assuming you have this switched on

window -> Preferences -> (Your code example) C/C++ -> Code Analysis -> No break at end of case

If you go to Customize Selected..., you will get "Comment text to suppress the problem [...]" which declares which data stream will actually only change the effect but still won't fix the problem.

The relevant line is in the file org.eclipse.cdt and looks like this :

org.eclipse.cdt.codan.internal.checkers.CaseBreakProblem.params={launchModes=>{RUN_ON_FULL_BUILD=>true,RUN_ON_INC_BUILD=>true,RUN_ON_FILE_OPEN=>false,RUN_ON_FILE_SAVE=>false,RUN_AS_YOU_TYPE=>true,RUN_ON_DEMAND=>true},suppression_comment=>"@suppress(\"No break at end of case\")",no_break_comment=>"no break",last_case_param=>false,empty_case_param=>false,enable_fallthrough_quickfix_param=>false}

  • Having this like in my example will work with the code below.

  • Note that the content of the comment does not matter, but it must contain an identical fragment corresponding to the settings contained in the Eclipse itself

  • Additionally, it is important to put the comment after the } character, unless it is a single argument case

  • Eclipse will still report the problem and this cannot be avoided in any way by having the bug report enabled in the settings, but it will no longer underline every line in the case

          switch(next) {
              case 0: {
                  if(true) {
                      //Do stuff...
                  } else {
                      break;
                  }
                  next = 1;
              }
              // ThiS Is The Line that CAUSE N"no break////////"  
              case 1: {
                  if(true) {
                      //Do stuff...
                  } else {
                      break;
                  }
                  next = 2;
              }
              case 2: {
                  if(true) {
                      //Wont do stuff...
                      //It will break here
                      break;
                  } else {
                      next = 3;
                  }
              }
    

I present a photo that shows the effect in the eclipse itself.

enter image description here

  • I read your whole answer and don't understand. Do you mean that "you can specify exactly which string specifies 'no break' in eclipse settings and if you don't have exactly that string then will still warn even if it says 'no break'. Look, here's an example of eclipse warning despite the string?" – c.fogelklou Sep 08 '21 at 04:43
  • this is exactly what my post shall clarify –  Nov 04 '21 at 10:46
-2

I have encountered this question,and I just want to eliminate them. I tried to add /* no break */,you should make sure it is added before the next "case". This is the question I have encountered

This is the solution I use:

MD XF
  • 7,860
  • 7
  • 40
  • 71
brain
  • 1
  • 2