Skip to content

Commit 070968b

Browse files
committed
Avoid blank return when statement is last in a function body
1 parent 7cc9c99 commit 070968b

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
#include "AvoidReturnWithVoidValueCheck.h"
1010
#include "../utils/BracesAroundStatement.h"
1111
#include "../utils/LexerUtils.h"
12-
#include "clang/AST/Stmt.h"
13-
#include "clang/ASTMatchers/ASTMatchFinder.h"
14-
#include "clang/ASTMatchers/ASTMatchers.h"
15-
#include "clang/Basic/Diagnostic.h"
16-
#include "clang/Lex/Lexer.h"
1712

1813
using namespace clang::ast_matchers;
1914

@@ -36,7 +31,10 @@ void AvoidReturnWithVoidValueCheck::registerMatchers(MatchFinder *Finder) {
3631
Finder->addMatcher(
3732
returnStmt(
3833
hasReturnValue(allOf(hasType(voidType()), unless(initListExpr()))),
39-
optionally(hasParent(compoundStmt().bind("compound_parent"))))
34+
optionally(hasParent(
35+
compoundStmt(
36+
optionally(hasParent(functionDecl().bind("function_parent"))))
37+
.bind("compound_parent"))))
4038
.bind("void_return"),
4139
this);
4240
}
@@ -65,9 +63,11 @@ void AvoidReturnWithVoidValueCheck::check(
6563
Diag << BraceInsertionHints.openingBraceFixIt()
6664
<< BraceInsertionHints.closingBraceFixIt();
6765
}
68-
Diag << FixItHint::CreateRemoval(VoidReturn->getReturnLoc())
69-
<< FixItHint::CreateInsertion(SemicolonPos.getLocWithOffset(1),
70-
" return;", true);
66+
Diag << FixItHint::CreateRemoval(VoidReturn->getReturnLoc());
67+
if (!Result.Nodes.getNodeAs<FunctionDecl>("function_parent") ||
68+
SurroundingBlock->body_back() != VoidReturn)
69+
Diag << FixItHint::CreateInsertion(SemicolonPos.getLocWithOffset(1),
70+
" return;", true);
7171
}
7272

7373
void AvoidReturnWithVoidValueCheck::storeOptions(

clang-tools-extra/test/clang-tidy/checkers/readability/avoid-return-with-void-value.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void f2() {
1212
return f1();
1313
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
1414
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
15-
// CHECK-FIXES: f1(); return;
15+
// CHECK-FIXES: f1();
1616
}
1717

1818
void f3(bool b) {
@@ -23,19 +23,19 @@ void f3(bool b) {
2323
return f2();
2424
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
2525
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
26-
// CHECK-FIXES: f2(); return;
27-
// CHECK-FIXES-LENIENT: f2(); return;
26+
// CHECK-FIXES: f2();
27+
// CHECK-FIXES-LENIENT: f2();
2828
}
2929

3030
template<class T>
3131
T f4() {}
3232

3333
void f5() {
34-
return f4<void>();
35-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
36-
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
37-
// CHECK-FIXES: f4<void>(); return;
38-
// CHECK-FIXES-LENIENT: f4<void>(); return;
34+
{ return f4<void>(); }
35+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
36+
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:7: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
37+
// CHECK-FIXES: { f4<void>(); return; }
38+
// CHECK-FIXES-LENIENT: { f4<void>(); return; }
3939
}
4040

4141
void f6() { return; }
@@ -48,8 +48,8 @@ void f9() {
4848
return (void)f7();
4949
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
5050
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
51-
// CHECK-FIXES: (void)f7(); return;
52-
// CHECK-FIXES-LENIENT: (void)f7(); return;
51+
// CHECK-FIXES: (void)f7();
52+
// CHECK-FIXES-LENIENT: (void)f7();
5353
}
5454

5555
#define RETURN_VOID return (void)1
@@ -77,6 +77,7 @@ VOID f13() {
7777
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
7878
// CHECK-FIXES: f12(); return;
7979
// CHECK-FIXES-LENIENT: f12(); return;
80+
(void)1;
8081
}
8182

8283
void f14() {
@@ -85,6 +86,7 @@ void f14() {
8586
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
8687
// CHECK-FIXES: /* comment */ f1() /* comment */ ; return;
8788
// CHECK-FIXES-LENIENT: /* comment */ f1() /* comment */ ; return;
89+
(void)1;
8890
}
8991

9092
void f15() {
@@ -93,6 +95,7 @@ void f15() {
9395
// CHECK-MESSAGES-LENIENT: :[[@LINE-2]]:5: warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
9496
// CHECK-FIXES: /*comment*/f1()/*comment*/; return;//comment
9597
// CHECK-FIXES-LENIENT: /*comment*/f1()/*comment*/; return;//comment
98+
(void)1;
9699
}
97100

98101
void f16(bool b) {

0 commit comments

Comments
 (0)