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

Warn on semicolons immediately following "else" #34667

Open
llvmbot opened this issue Nov 15, 2017 · 6 comments
Open

Warn on semicolons immediately following "else" #34667

llvmbot opened this issue Nov 15, 2017 · 6 comments
Labels
bugzilla Issues migrated from bugzilla c++ clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 15, 2017

Bugzilla Link 35319
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @dwblaikie,@DougGregor,@rnk,@Weverything
Fixed by commit(s) r318456

Extended Description

clang version 6.0.0 (trunk 317263)

This code:

    int main(int argc, char** argv) {
      if (argc > 4);   // <-- Note semicolon!
      return 0;
    }

Gives:

    eraseme.cc:2:16: warning: if statement has empty body [-Wempty-body]
      if (argc > 4);
                   ^
    eraseme.cc:2:16: note: put the semicolon on a separate line to silence this
          warning
    1 warning generated.

This is good! And it also works for else if cases. But I just did the same bug and put a semicolon after a bare else:

    int main(int argc, char** argv) {
      if (argc > 4)
        return 1;
      else;  // <- Semicolon
        return 2;
      return 0;
    }

And there is no warning. This was pretty tricky to find and it seems like the same rule for if semicolons should just also be applied to else.

@rnk
Copy link
Collaborator

rnk commented Nov 16, 2017

Thanks, should be fixed by r318456.

@rnk
Copy link
Collaborator

rnk commented Mar 1, 2018

This was reverted.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@Quuxplusone Quuxplusone added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Jan 17, 2022
@wheatman wheatman added clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party labels Oct 19, 2023
@llvmbot
Copy link
Collaborator Author

llvmbot commented Oct 19, 2023

@llvm/issue-subscribers-clang-frontend

Author: None (llvmbot)

| | | | --- | --- | | Bugzilla Link | [35319](https://llvm.org/bz35319) | | Version | trunk | | OS | Linux | | Reporter | LLVM Bugzilla Contributor | | CC | @dwblaikie,@DougGregor,@rnk,@Weverything | | Fixed by commit(s) | r318456 |

Extended Description

clang version 6.0.0 (trunk 317263)

This code:

int main(int argc, char** argv) {
  if (argc &gt; 4);   // &lt;-- Note semicolon!
  return 0;
}

Gives:

eraseme.cc:2:16: warning: if statement has empty body [-Wempty-body]
  if (argc &gt; 4);
               ^
eraseme.cc:2:16: note: put the semicolon on a separate line to silence this
      warning
1 warning generated.

This is good! And it also works for "else if" cases. But I just did the same bug and put a semicolon after a bare else:

int main(int argc, char** argv) {
  if (argc &gt; 4)
    return 1;
  else;  // &lt;- Semicolon
    return 2;
  return 0;
}

And there is no warning. This was pretty tricky to find and it seems like the same rule for "if" semicolons should just also be applied to "else".

@wheatman
Copy link
Contributor

confirming that this is still the case in post 17 trunk(5a56f00)
https://godbolt.org/z/nqoqWeExb

code

int main(int argc, char** argv) {
  if (argc > 4)
    return 1;
  else;  // <- Semicolon
    return 2;
  return 0;
}

no warning given for clang

@shafik
Copy link
Collaborator

shafik commented Oct 19, 2023

Commit that attempted to fix this: adefb76

Commit that revert it says it was reverted it: 59ad150

says:

This caused warnings also when the if or else comes from macros.

So someone can likely pick up the original fix and needs to add tests for macros to make sure this does not break likely existing code.

@Endilll Endilll removed the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jan 20, 2024
@shafik
Copy link
Collaborator

shafik commented Jan 26, 2024

Also see 9541975 for an example for when the original fix that was reverted failed on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++ clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

6 participants