[clang-tools-extra] 498c885 - [clang-tidy] Fix if-constexpr false-positive in readability-misleading-indentation

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 29 09:09:20 PDT 2023


Author: Piotr Zegar
Date: 2023-03-29T16:08:54Z
New Revision: 498c88563b7f6379d9e6ae4d89d182756fa1c0bc

URL: https://github.com/llvm/llvm-project/commit/498c88563b7f6379d9e6ae4d89d182756fa1c0bc
DIFF: https://github.com/llvm/llvm-project/commit/498c88563b7f6379d9e6ae4d89d182756fa1c0bc.diff

LOG: [clang-tidy] Fix if-constexpr false-positive in readability-misleading-indentation

When  depend on template parameter,
compiler can use NullStmt instead of CompoundStmt.
This causes issues as we losing information about
end location of that Stmt. To avoid this issue
check now ignores ifStmt with NullStmt on true-branch.

Fixes: https://github.com/llvm/llvm-project/issues/61435

Reviewed By: carlosgalvezp

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

Added: 
    clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation-cpp17.cpp

Modified: 
    clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
index 4c10b6f6eebe0..2c011f5c0e690 100644
--- a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
@@ -104,7 +104,8 @@ void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
 }
 
 void MisleadingIndentationCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(ifStmt(hasElse(stmt())).bind("if"), this);
+  Finder->addMatcher(
+      ifStmt(unless(hasThen(nullStmt())), hasElse(stmt())).bind("if"), this);
   Finder->addMatcher(
       compoundStmt(has(stmt(anyOf(ifStmt(), forStmt(), whileStmt()))))
           .bind("compound"),

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 03ba9e160b738..413f8925aec2c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -240,6 +240,10 @@ Changes in existing checks
   magic numbers in type aliases such as ``using`` and ``typedef`` declarations if
   the new ``IgnoreTypeAliases`` option is set to true.
 
+- Fixed a false positive in :doc:`readability-misleading-indentation
+  <clang-tidy/checks/readability/misleading-indentation>` check when warning would
+  be unnecessarily emitted for template dependent ``if constexpr``.
+
 - Fixed a false positive in :doc:`cppcoreguidelines-slicing
   <clang-tidy/checks/cppcoreguidelines/slicing>` check when warning would be
   emitted in constructor for virtual base class initialization.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation-cpp17.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation-cpp17.cpp
new file mode 100644
index 0000000000000..31e3893a3e70b
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation-cpp17.cpp
@@ -0,0 +1,29 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s readability-misleading-indentation %t -- -- -fno-delayed-template-parsing
+
+namespace PR61435 {
+
+template<int N>
+constexpr auto lam_correct = []{
+  if constexpr (N == 1) {
+  } else {
+  }
+};
+
+template<int N>
+constexpr auto lam_incorrect = []{
+  if constexpr (N == 1) {
+  }
+   else {
+  }
+  // CHECK-MESSAGES: :[[@LINE-2]]:4: warning: 
diff erent indentation for 'if' and corresponding 'else' [readability-misleading-indentation]
+};
+
+void test() {
+  lam_correct<1>();
+  lam_correct<2>();
+
+  lam_incorrect<1>();
+  lam_incorrect<2>();
+}
+
+}


        


More information about the cfe-commits mailing list