LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 22785 - readability-braces-around-statements doesn't work well with macros
Summary: readability-braces-around-statements doesn't work well with macros
Status: RESOLVED WORKSFORME
Alias: None
Product: clang-tools-extra
Classification: Unclassified
Component: clang-tidy (show other bugs)
Version: unspecified
Hardware: PC Linux
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-04 05:20 PST by Alexander Kornienko
Modified: 2021-10-08 01:34 PDT (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Kornienko 2015-03-04 05:20:44 PST
$ cat test.cc
#define MACRO_1 i++
#define MACRO_2

void test(int i)
{
        if( i % 3) i--; // OK
        else if( i % 2) MACRO_1; // KO, no braces
        else MACRO_2; // OK
}
$ clang_tidy -checks=-*,readability-braces-around-statements -fix test.cc --
2 warnings generated.
test.cc:6:19: warning: statement should be inside braces [readability-braces-around-statements]
        if( i % 3) i--; // OK
                  ^
test.cc:6:19: note: FIX-IT applied suggested code changes
        if( i % 3) i--; // OK
                  ^
test.cc:7:9: note: FIX-IT applied suggested code changes
        else if( i % 2) MACRO_1; // KO, no braces
        ^
test.cc:8:13: warning: statement should be inside braces [readability-braces-around-statements]
        else MACRO_2; // OK
            ^
test.cc:8:13: note: FIX-IT applied suggested code changes
        else MACRO_2; // OK
            ^
test.cc:8:28: note: FIX-IT applied suggested code changes
        else MACRO_2; // OK
                           ^
clang-tidy applied 4 of 4 suggested fixes.
Comment 1 Alexander Kornienko 2015-05-15 13:11:57 PDT
Another test case:

$ cat test.cc
#define M(x) x
int f(bool b) {
  if (b)
    return 1;
  else
    return 2;
  if (b)
    return 1;
  else
    M(return 2);
}
$ clang_tidy -fix  test.cc -- 
3 warnings generated.
...
clang-tidy applied 6 of 6 suggested fixes.
$ cat test.cc
#define M(x) x
int f(bool b) {
  if (b) {
    return 1;
  } else {
    return 2;
}
  if (b) {
    return 1;
  } else
    M(return 2);
}
Comment 2 Alan Copy 2021-10-08 01:34:49 PDT
Hello,

This two examples works fine with LLVM 11.0.0.