Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang++] Unnecessary warning "non-void function does not return a value in all control paths" #111509

Open
mkornaukhov03 opened this issue Oct 8, 2024 · 2 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not

Comments

@mkornaukhov03
Copy link

I have the following snippet:

int foo(int) {
    bool matched = false;
    do {
      if (matched) {
        break;
      }
        return 42;
    } while(false);
}


int main() {
    foo(42);
}

I compile it with clang++-18:

$ clang++-18 --version
Ubuntu clang version 18.1.8 (++20240731024944+3b5b5c1ec4a3-1~exp1~20240731145000.144)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ clang++-18 -Wreturn-type main.cpp 
main.cpp:9:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
    9 | }
      | ^
1 warning generated.

Variable matched is effectively constant and there are no paths that "do not return a value".
The expected behavior is such that there should be no such warning.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Oct 8, 2024
@chrchr-github
Copy link

Reduced:

int foo() {
    /*const*/ bool b = true;
    if (b) 
        return 42;
}

const b prevents the warning.

@mkornaukhov03
Copy link
Author

Initial snippet was something like that:

int foo(int x) {
    bool matched = false;
    do {
      if (matched) {
        label: return 0;
      }

      if (matched || x == 1) {
        matched = true;
        return 123;
      }
      if (matched || x == 2) {
        matched = true;
        return 456;
      }

      if (matched) {
        break;
      }
      goto label;
    } while(false);
}

Which shouldn't have warning, too. But I reduced it to the snippet that is in description.
In fact even this things:

int foo(int x) {
    bool matched = false;

    if (matched) return 1;
    if (!matched) return 2;
}

emits warning.

@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not and removed clang Clang issues not falling into any other category labels Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

3 participants