-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy] Make readability-redundant-control-flow not suggest deleting unrelated lines
#171287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
localspook
wants to merge
2
commits into
llvm:main
Choose a base branch
from
localspook:readability-redundant-control-flow-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[clang-tidy] Make readability-redundant-control-flow not suggest deleting unrelated lines
#171287
localspook
wants to merge
2
commits into
llvm:main
from
localspook:readability-redundant-control-flow-fix
+60
−13
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…leting unrelated lines
Member
|
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Victor Chernyakin (localspook) ChangesCloses #171200. Full diff: https://github.com/llvm/llvm-project/pull/171287.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantControlFlowCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantControlFlowCheck.cpp
index 132b7ddc4311b..b77108c49f53c 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantControlFlowCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantControlFlowCheck.cpp
@@ -71,19 +71,11 @@ void RedundantControlFlowCheck::issueDiagnostic(
if (isLocationInMacroExpansion(SM, StmtRange.getBegin()))
return;
- const CompoundStmt::const_reverse_body_iterator Previous =
- ++Block->body_rbegin();
- SourceLocation Start;
- if (Previous != Block->body_rend())
- Start = Lexer::findLocationAfterToken(
- cast<Stmt>(*Previous)->getEndLoc(), tok::semi, SM, getLangOpts(),
- /*SkipTrailingWhitespaceAndNewLine=*/true);
- if (!Start.isValid())
- Start = StmtRange.getBegin();
- auto RemovedRange = CharSourceRange::getCharRange(
- Start, Lexer::findLocationAfterToken(
- StmtRange.getEnd(), tok::semi, SM, getLangOpts(),
- /*SkipTrailingWhitespaceAndNewLine=*/true));
+ const auto RemovedRange = CharSourceRange::getCharRange(
+ StmtRange.getBegin(),
+ Lexer::findLocationAfterToken(StmtRange.getEnd(), tok::semi, SM,
+ getLangOpts(),
+ /*SkipTrailingWhitespaceAndNewLine=*/true));
diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index d1fb1cba3e45a..9f72f31b3cfa9 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -581,6 +581,11 @@ Changes in existing checks
<clang-tidy/checks/readability/redundant-casting>` check by fixing false
negatives when explicitly cast from function pointer.
+- Improved :doc:`readability-redundant-control-flow
+ <clang-tidy/checks/readability/redundant-control-flow>` by fixing an issue
+ where the check would sometimes suggest deleting not only a redundant
+ ``return`` or ``continue``, but also unrelated lines preceding it.
+
- Improved :doc:`readability-uppercase-literal-suffix
<clang-tidy/checks/readability/uppercase-literal-suffix>` check to recognize
literal suffixes added in C++23 and C23.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-control-flow.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-control-flow.cpp
index 77644902e7772..0083b5e921bc9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-control-flow.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-control-flow.cpp
@@ -222,3 +222,33 @@ void call_templates() {
template_loop(10L);
template_loop(10U);
}
+
+void dont_delete_lines_before_return_statement() {
+ do {} while (0);
+#ifdef FOO
+#endif
+ return;
+}
+// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: redundant return statement
+// CHECK-FIXES: void dont_delete_lines_before_return_statement() {
+// CHECK-FIXES-NEXT: do {} while (0);
+// CHECK-FIXES-NEXT: #ifdef FOO
+// CHECK-FIXES-NEXT: #endif
+// CHECK-FIXES-NEXT: }
+
+void dont_delete_lines_before_continue_statement() {
+ for (;;) {
+ do {} while (0);
+#ifdef BAR
+#endif
+ continue;
+ }
+}
+// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: redundant continue statement
+// CHECK-FIXES: void dont_delete_lines_before_continue_statement() {
+// CHECK-FIXES-NEXT: for (;;) {
+// CHECK-FIXES-NEXT: do {} while (0);
+// CHECK-FIXES-NEXT: #ifdef BAR
+// CHECK-FIXES-NEXT: #endif
+// CHECK-FIXES-NEXT: }
+// CHECK-FIXES-NEXT: }
|
vbvictor
approved these changes
Dec 9, 2025
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-control-flow.cpp
Show resolved
Hide resolved
zeyi2
approved these changes
Dec 10, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #171200.