[all-commits] [llvm/llvm-project] 617e8e: [clang-tidy] ElseAfterReturn check wont suggest fi...

Nathan James via All-commits all-commits at lists.llvm.org
Thu Nov 19 10:20:53 PST 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 617e8e5ee3bb3316baae7a69d74b5ff95031d571
      https://github.com/llvm/llvm-project/commit/617e8e5ee3bb3316baae7a69d74b5ff95031d571
  Author: Nathan James <n.james93 at hotmail.co.uk>
  Date:   2020-11-19 (Thu, 19 Nov 2020)

  Changed paths:
    M clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
    M clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.h
    A clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return-pp-no-crash.cpp
    M clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return.cpp

  Log Message:
  -----------
  [clang-tidy] ElseAfterReturn check wont suggest fixes if preprocessor branches are involved

Consider this code:
```
if (Cond) {
#ifdef X_SUPPORTED
  X();
#else
  return;
#endif
} else {
  Y();
}
Z();```

In this example, if `X_SUPPORTED` is not defined, currently we'll get a warning from the else-after-return check. However If we apply that fix, and then the code is recompiled with `X_SUPPORTED` defined, we have inadvertently changed the behaviour of the if statement due to the else being removed. Code flow when `Cond` is `true` will be:
```
X();
Y();
Z();```
 where as before the fix it was:
 ```
 X();
 Z();```

 This patch adds checks that guard against `#endif` directives appearing between the control flow interrupter and the else and not applying the fix if they are detected.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D91485




More information about the All-commits mailing list