Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:49:43+00:00 2026-05-27T13:49:43+00:00

Given the following code: bool doGoodThing; switch (some_variable) { case 1: case 2: doGoodThing

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:49:44+00:00Added an answer on May 27, 2026 at 1:49 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code (that doesn't work): while True: # Snip: print out current
Given the following code, is there a way I can call class A's version
Question: Given the following code snippet: bool foo(int n) { for(int i=3;i<sqrt(n)+0.5;i+=2) { if((n%i)==0){
Given the following code: public static bool UpdateUser(string userId, string jobTitle) { return GetProvider().UpdateUser
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
given the following code: import ctypes ip=192.168.1.1 thisdll = ctypes.cdll['aDLL'] thisdll.functionThatExpectsAnIP(ip) how can I
Given the following code: var people = new List<person>(){ new person { Name =
Given the following code,the hover function works correctly, but when clicked, loses the clicked_no_event_box
Given the following code: using (var client = new WebClient()) { string url =
Given the following code $c= new SoapClient('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL'); $usa = USD; $eng = GBP; doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.