diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp @@ -54,13 +54,17 @@ // 2) inside a range-for over an array // 3) if it converts a string literal to a pointer Finder->addMatcher( - traverse(TK_AsIs, - implicitCastExpr( - unless(hasParent(arraySubscriptExpr())), - unless(hasParentIgnoringImpCasts(explicitCastExpr())), - unless(isInsideOfRangeBeginEndStmt()), - unless(hasSourceExpression(ignoringParens(stringLiteral())))) - .bind("cast")), + traverse( + TK_AsIs, + implicitCastExpr( + unless(hasParent(arraySubscriptExpr())), + unless(hasParentIgnoringImpCasts(explicitCastExpr())), + unless(isInsideOfRangeBeginEndStmt()), + unless(hasSourceExpression(ignoringParens(stringLiteral()))), + unless(hasSourceExpression(ignoringParens(conditionalOperator( + allOf(hasTrueExpression(stringLiteral()), + hasFalseExpression(stringLiteral()))))))) + .bind("cast")), this); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -67,6 +67,10 @@ Improvements to clang-tidy -------------------------- +- Make the `cppcoreguidelines-pro-bounds-array-to-pointer-decay` check accept + string literal to pointer decay in conditional operator even if operands are + of the same length. + - Ignore warnings from macros defined in system headers, if not using the `-system-headers` flag. diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -49,3 +49,14 @@ void *a[2]; f2(static_cast(a)); // OK, explicit cast } + +void issue31155(int i) { + const char *a = i ? "foo" : "bar"; // OK, decay string literal to pointer + const char *b = i ? "foo" : "foobar"; // OK, decay string literal to pointer + + char arr[1]; + const char *c = i ? arr : "bar"; + // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not implicitly decay an array into a pointer + const char *d = i ? "foo" : arr; + // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: do not implicitly decay an array into a pointer +}