[clang-tools-extra] ba129c7 - [clang-tidy] Disable match on `if constexpr` statements in template instantiation for `readability-misleading-indentation` check.

Andi-Bogdan Postelnicu via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 8 06:39:36 PST 2020


Author: Andi-Bogdan Postelnicu
Date: 2020-01-08T16:36:13+02:00
New Revision: ba129c7d0f5c7c32398ad708c88e14cb06a339ad

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

LOG: [clang-tidy] Disable match on `if constexpr` statements in template instantiation for `readability-misleading-indentation` check.

Summary: Fixes fixes `readability-misleading-identation` for `if constexpr`. This is very similar to D71980.

Reviewers: alexfh

Subscribers: xazax.hun, cfe-commits

Tags: #clang

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability-misleading-indentation.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
index 0fd5c1fc55c6..3167d159b74f 100644
--- a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
@@ -106,7 +106,11 @@ void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
 }
 
 void MisleadingIndentationCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(ifStmt(hasElse(stmt())).bind("if"), this);
+  Finder->addMatcher(
+      ifStmt(allOf(hasElse(stmt()),
+                   unless(allOf(isConstexpr(), isInTemplateInstantiation()))))
+          .bind("if"),
+      this);
   Finder->addMatcher(
       compoundStmt(has(stmt(anyOf(ifStmt(), forStmt(), whileStmt()))))
           .bind("compound"),

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-misleading-indentation.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-misleading-indentation.cpp
index 403994923549..7ceb0cb42cfe 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-misleading-indentation.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-misleading-indentation.cpp
@@ -2,6 +2,7 @@
 
 void foo1();
 void foo2();
+void foo3();
 
 #define BLOCK \
   if (cond1)  \
@@ -118,3 +119,79 @@ void g(bool x) {
   #pragma unroll
   for (int k = 0; k < 1; ++k) {}
 }
+
+template<bool b>
+void mustPass() {
+  if constexpr (b) {
+    foo1();
+  } else {
+    foo2();
+  }
+}
+
+void mustPassNonTemplate() {
+  constexpr unsigned Value = 1;
+  if constexpr (Value == 0) {
+    foo1();
+  } else if constexpr (Value == 1) {
+    foo2();
+  } else {
+    foo3();
+  }
+}
+
+template<bool b>
+void mustFail() {
+  if constexpr (b) {
+    foo1();
+  }
+    else {
+      foo2();
+      // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: 
diff erent indentation for 'if' and corresponding 'else' [readability-misleading-indentation]
+  }
+}
+
+void mustFailNonTemplate() {
+  constexpr unsigned Value = 1;
+  if constexpr (Value == 0) {
+    foo1();
+  }
+    else {
+  foo2();
+  // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: 
diff erent indentation for 'if' and corresponding 'else' [readability-misleading-indentation]
+  }
+
+  if constexpr (Value == 0)
+    foo1();
+    else
+  foo2();
+  // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: 
diff erent indentation for 'if' and corresponding 'else' [readability-misleading-indentation]
+}
+
+template<bool b>
+void mustFailNoInsta() {
+  if constexpr (b) {
+    foo1();
+  }
+    else {
+      foo2();
+      // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: 
diff erent indentation for 'if' and corresponding 'else' [readability-misleading-indentation]
+  }
+}
+
+template<bool b>
+void mustPassNoInsta() {
+  if constexpr (b) {
+    foo1();
+  }
+  else {
+    foo2();
+  }
+}
+
+void call() {
+  mustPass<true>();
+  mustPass<false>();
+  mustFail<true>();
+  mustFail<false>();
+}


        


More information about the cfe-commits mailing list