-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy]Add new check readability-avoid-nested-conditional-operator #78022
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
Merged
HerrCai0907
merged 4 commits into
main
from
users/ccc/create-AvoidnestedternaryconditionaloperatorCheck
Jan 15, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0988bb2
[clang-tidy]Add new check readability-avoid-nested-conditional-operator
HerrCai0907 f9736f2
fix comments
HerrCai0907 aeeefb0
Merge branch 'main' of https://github.com/llvm/llvm-project into user…
HerrCai0907 9a831c9
update release note
HerrCai0907 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
clang-tools-extra/clang-tidy/readability/AvoidNestedConditionalOperatorCheck.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//===--- AvoidNestedConditionalOperatorCheck.cpp - clang-tidy ----------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "AvoidNestedConditionalOperatorCheck.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/ASTMatchers/ASTMatchers.h" | ||
#include "clang/Basic/DiagnosticIDs.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::readability { | ||
|
||
void AvoidNestedConditionalOperatorCheck::registerMatchers( | ||
MatchFinder *Finder) { | ||
Finder->addMatcher( | ||
conditionalOperator( | ||
PiotrZSL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
anyOf( | ||
hasCondition(ignoringParenCasts( | ||
PiotrZSL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
conditionalOperator().bind("nested-conditional-operator"))), | ||
hasTrueExpression(ignoringParenCasts( | ||
conditionalOperator().bind("nested-conditional-operator"))), | ||
hasFalseExpression(ignoringParenCasts( | ||
conditionalOperator().bind("nested-conditional-operator"))))) | ||
.bind("conditional-operator"), | ||
this); | ||
} | ||
|
||
void AvoidNestedConditionalOperatorCheck::check( | ||
const MatchFinder::MatchResult &Result) { | ||
const auto *CO = | ||
Result.Nodes.getNodeAs<ConditionalOperator>("conditional-operator"); | ||
const auto *NCO = Result.Nodes.getNodeAs<ConditionalOperator>( | ||
"nested-conditional-operator"); | ||
assert(CO); | ||
assert(NCO); | ||
|
||
if (CO->getBeginLoc().isMacroID() || NCO->getBeginLoc().isMacroID()) | ||
return; | ||
|
||
diag(NCO->getBeginLoc(), | ||
"conditional operator is used as sub-expression of parent conditional " | ||
"operator, refrain from using nested conditional operators"); | ||
diag(CO->getBeginLoc(), "parent conditional operator here", | ||
DiagnosticIDs::Note); | ||
} | ||
|
||
} // namespace clang::tidy::readability |
33 changes: 33 additions & 0 deletions
33
clang-tools-extra/clang-tidy/readability/AvoidNestedConditionalOperatorCheck.h
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===--- AvoidNestedConditionalOperatorCheck.h - clang-tidy -----*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AVOID_NESTED_CONDITIONAL_OPERATOR_CHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AVOID_NESTED_CONDITIONAL_OPERATOR_CHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::readability { | ||
|
||
/// Identifies instances of nested conditional operators in the code. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/readability/avoid-nested-conditional-operator.html | ||
class AvoidNestedConditionalOperatorCheck : public ClangTidyCheck { | ||
public: | ||
AvoidNestedConditionalOperatorCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TK_IgnoreUnlessSpelledInSource; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::readability | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AVOID_NESTED_CONDITIONAL_OPERATOR_CHECK_H |
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
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
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
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
21 changes: 21 additions & 0 deletions
21
...-extra/docs/clang-tidy/checks/readability/avoid-nested-conditional-operator.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.. title:: clang-tidy - readability-avoid-nested-conditional-operator | ||
|
||
readability-avoid-nested-conditional-operator | ||
============================================= | ||
|
||
Identifies instances of nested conditional operators in the code. | ||
|
||
Nested conditional operators, also known as ternary operators, can contribute | ||
to reduced code readability and comprehension. So they should be split as | ||
several statements and stored the intermediate results in temporary variable. | ||
|
||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Examples: | ||
|
||
.. code-block:: c++ | ||
|
||
int NestInConditional = (condition1 ? true1 : false1) ? true2 : false2; | ||
int NestInTrue = condition1 ? (condition2 ? true1 : false1) : false2; | ||
int NestInFalse = condition1 ? true1 : condition2 ? true2 : false1; | ||
|
||
This check implements part of `AUTOSAR C++14 Rule A5-16-1 | ||
<https://www.autosar.org/fileadmin/standards/R22-11/AP/AUTOSAR_RS_CPP14Guidelines.pdf>`_. |
24 changes: 24 additions & 0 deletions
24
clang-tools-extra/test/clang-tidy/checkers/readability/avoid-nested-conditional-operator.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// RUN: %check_clang_tidy %s readability-avoid-nested-conditional-operator %t | ||
|
||
int NestInConditional = (true ? true : false) ? 1 : 2; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators | ||
// CHECK-MESSAGES: :[[@LINE-2]]:25: note: parent conditional operator here | ||
|
||
int NestInTrue = true ? (true ? 1 : 2) : 2; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators | ||
// CHECK-MESSAGES: :[[@LINE-2]]:18: note: parent conditional operator here | ||
|
||
int NestInFalse = true ? 1 : true ? 1 : 2; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators | ||
// CHECK-MESSAGES: :[[@LINE-2]]:19: note: parent conditional operator here | ||
int NestInFalse2 = true ? 1 : (true ? 1 : 2); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators | ||
// CHECK-MESSAGES: :[[@LINE-2]]:20: note: parent conditional operator here | ||
|
||
int NestWithParensis = true ? 1 : ((((true ? 1 : 2)))); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators | ||
// CHECK-MESSAGES: :[[@LINE-2]]:24: note: parent conditional operator here | ||
|
||
#define CONDITIONAL_EXPR (true ? 1 : 2) | ||
// not diag for macro since it will not reduce readability | ||
int NestWithMacro = true ? CONDITIONAL_EXPR : 2; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add
-
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 10602c2