Skip to content

Commit 505434b

Browse files
committedOct 14, 2016
[clang-tidy] Fix readability-braces-around-statements false positive
Summary: This fixes a false-positive e.g. when string literals are returned from if statement. This patch includes as well a small fix to includes and renames of the test suite that collided with the name of the check. Reviewers: alexfh, hokein Subscribers: hokein Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D25558 llvm-svn: 284212
1 parent 174d2e7 commit 505434b

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed
 

‎clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "UseUsingCheck.h"
11-
#include "../utils/LexerUtils.h"
11+
#include "clang/AST/ASTContext.h"
12+
#include "clang/Lex/Lexer.h"
1213

1314
using namespace clang::ast_matchers;
1415

‎clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ SourceLocation findEndLocation(SourceLocation LastTokenLoc,
6161
bool SkipEndWhitespaceAndComments = true;
6262
tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
6363
if (TokKind == tok::NUM_TOKENS || TokKind == tok::semi ||
64-
TokKind == tok::r_brace) {
64+
TokKind == tok::r_brace || isStringLiteral(TokKind)) {
6565
// If we are at ";" or "}", we found the last token. We could use as well
6666
// `if (isa<NullStmt>(S))`, but it wouldn't work for nested statements.
6767
SkipEndWhitespaceAndComments = false;

‎clang-tools-extra/unittests/clang-tidy/ReadabilityModuleTest.cpp

+30-16
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ TEST(NamespaceCommentCheckTest, Basic) {
1515
runCheckOnCode<NamespaceCommentCheck>("namespace i {\n}"));
1616
EXPECT_EQ("namespace {\n} // namespace",
1717
runCheckOnCode<NamespaceCommentCheck>("namespace {\n}"));
18-
EXPECT_EQ(
19-
"namespace i { namespace j {\n} // namespace j\n } // namespace i",
20-
runCheckOnCode<NamespaceCommentCheck>("namespace i { namespace j {\n} }"));
18+
EXPECT_EQ("namespace i { namespace j {\n} // namespace j\n } // namespace i",
19+
runCheckOnCode<NamespaceCommentCheck>(
20+
"namespace i { namespace j {\n} }"));
2121
}
2222

2323
TEST(NamespaceCommentCheckTest, SingleLineNamespaces) {
@@ -49,10 +49,11 @@ TEST(NamespaceCommentCheckTest, CheckExistingComments) {
4949
"} // Anonymous namespace.",
5050
runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
5151
"} // Anonymous namespace."));
52-
EXPECT_EQ("namespace q {\n"
53-
"} // namespace q",
54-
runCheckOnCode<NamespaceCommentCheck>("namespace q {\n"
55-
"} // anonymous namespace q"));
52+
EXPECT_EQ(
53+
"namespace q {\n"
54+
"} // namespace q",
55+
runCheckOnCode<NamespaceCommentCheck>("namespace q {\n"
56+
"} // anonymous namespace q"));
5657
EXPECT_EQ(
5758
"namespace My_NameSpace123 {\n"
5859
"} // namespace My_NameSpace123",
@@ -97,7 +98,7 @@ TEST(NamespaceCommentCheckTest, FixWrongComments) {
9798
"} // random text"));
9899
}
99100

100-
TEST(BracesAroundStatementsCheck, IfWithComments) {
101+
TEST(BracesAroundStatementsCheckTest, IfWithComments) {
101102
EXPECT_EQ("int main() {\n"
102103
" if (false /*dummy token*/) {\n"
103104
" // comment\n"
@@ -134,7 +135,7 @@ TEST(BracesAroundStatementsCheck, IfWithComments) {
134135
"}"));
135136
}
136137

137-
TEST(BracesAroundStatementsCheck, If) {
138+
TEST(BracesAroundStatementsCheckTest, If) {
138139
EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
139140
" if (false) {\n"
140141
" return -1;\n"
@@ -235,7 +236,7 @@ TEST(BracesAroundStatementsCheck, If) {
235236
"}"));
236237
}
237238

238-
TEST(BracesAroundStatementsCheck, IfElseWithShortStatements) {
239+
TEST(BracesAroundStatementsCheckTest, IfElseWithShortStatements) {
239240
ClangTidyOptions Options;
240241
Options.CheckOptions["test-check-0.ShortStatementLines"] = "1";
241242

@@ -269,7 +270,7 @@ TEST(BracesAroundStatementsCheck, IfElseWithShortStatements) {
269270
nullptr, "input.cc", None, Options));
270271
}
271272

272-
TEST(BracesAroundStatementsCheck, For) {
273+
TEST(BracesAroundStatementsCheckTest, For) {
273274
EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
274275
" for (;;) {\n"
275276
" ;\n"
@@ -304,7 +305,7 @@ TEST(BracesAroundStatementsCheck, For) {
304305
"}"));
305306
}
306307

307-
TEST(BracesAroundStatementsCheck, ForRange) {
308+
TEST(BracesAroundStatementsCheckTest, ForRange) {
308309
EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
309310
" int arr[4];\n"
310311
" for (int i : arr) {\n"
@@ -329,7 +330,7 @@ TEST(BracesAroundStatementsCheck, ForRange) {
329330
"}"));
330331
}
331332

332-
TEST(BracesAroundStatementsCheck, DoWhile) {
333+
TEST(BracesAroundStatementsCheckTest, DoWhile) {
333334
EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
334335
" do {\n"
335336
" ;\n"
@@ -347,7 +348,7 @@ TEST(BracesAroundStatementsCheck, DoWhile) {
347348
"}"));
348349
}
349350

350-
TEST(BracesAroundStatementsCheck, While) {
351+
TEST(BracesAroundStatementsCheckTest, While) {
351352
EXPECT_NO_CHANGES(BracesAroundStatementsCheck, "int main() {\n"
352353
" while (false) {\n"
353354
" ;\n"
@@ -411,7 +412,7 @@ TEST(BracesAroundStatementsCheck, While) {
411412
"}"));
412413
}
413414

414-
TEST(BracesAroundStatementsCheck, Nested) {
415+
TEST(BracesAroundStatementsCheckTest, Nested) {
415416
EXPECT_EQ("int main() {\n"
416417
" do { if (true) {}} while (false);\n"
417418
"}",
@@ -446,7 +447,7 @@ TEST(BracesAroundStatementsCheck, Nested) {
446447
"}"));
447448
}
448449

449-
TEST(BracesAroundStatementsCheck, Macros) {
450+
TEST(BracesAroundStatementsCheckTest, Macros) {
450451
EXPECT_NO_CHANGES(BracesAroundStatementsCheck,
451452
"#define IF(COND) if (COND) return -1;\n"
452453
"int main() {\n"
@@ -480,6 +481,19 @@ TEST(BracesAroundStatementsCheck, Macros) {
480481
"}"));
481482
}
482483

484+
#define EXPECT_NO_CHANGES_WITH_OPTS(Check, Opts, Code) \
485+
EXPECT_EQ(Code, runCheckOnCode<Check>(Code, nullptr, "input.cc", None, Opts))
486+
TEST(BracesAroundStatementsCheckTest, ImplicitCastInReturn) {
487+
ClangTidyOptions Opts;
488+
Opts.CheckOptions["test-check-0.ShortStatementLines"] = "1";
489+
490+
EXPECT_NO_CHANGES_WITH_OPTS(BracesAroundStatementsCheck, Opts,
491+
"const char *f() {\n"
492+
" if (true) return \"\";\n"
493+
" return \"abc\";\n"
494+
"}\n");
495+
}
496+
483497
} // namespace test
484498
} // namespace tidy
485499
} // namespace clang

0 commit comments

Comments
 (0)
Please sign in to comment.