Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a18634b

Browse files
committed
[clang-tidy] Never consider assignments as equivalent in misc-redundant-expression check
Fixes #35853. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D122535
1 parent 4542841 commit a18634b

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
134134
return cast<UnaryOperator>(Left)->getOpcode() ==
135135
cast<UnaryOperator>(Right)->getOpcode();
136136
case Stmt::BinaryOperatorClass:
137+
if (cast<BinaryOperator>(Left)->isAssignmentOp())
138+
return false;
137139
return cast<BinaryOperator>(Left)->getOpcode() ==
138140
cast<BinaryOperator>(Right)->getOpcode();
139141
case Stmt::UnaryExprOrTypeTraitExprClass:

clang-tools-extra/docs/ReleaseNotes.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Changes in existing checks
132132
the vector is a member of a structure.
133133

134134
- Fixed a false positive in :doc:`readability-non-const-parameter
135-
<clang-tidy/checks/readability-non-const-parameter>` when the parameter is referenced by an lvalue
135+
<clang-tidy/checks/readability-non-const-parameter>` when the parameter is referenced by an lvalue.
136136

137137
- Fixed a crash in :doc:`readability-const-return-type
138138
<clang-tidy/checks/readability-const-return-type>` when a pure virtual function
@@ -150,6 +150,9 @@ Changes in existing checks
150150
Fixed an issue when there was already an initializer in the constructor and
151151
the check would try to create another initializer for the same member.
152152

153+
- Fixed a false positive in :doc:`misc-redundant-expression <clang-tidy/checks/misc-redundant-expression>`
154+
involving assignments in conditions. This fixes `Issue 35853 <https://github.com/llvm/llvm-project/issues/35853>`_.
155+
153156
Removed checks
154157
^^^^^^^^^^^^^^
155158

clang-tools-extra/test/clang-tidy/checkers/misc-redundant-expression.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,15 @@ struct Bar2 {
831831
};
832832

833833
} // namespace no_crash
834+
835+
int TestAssignSideEffect(int i) {
836+
int k = i;
837+
838+
if ((k = k + 1) != 1 || (k = k + 1) != 2)
839+
return 0;
840+
841+
if ((k = foo(0)) != 1 || (k = foo(0)) != 2)
842+
return 1;
843+
844+
return 2;
845+
}

0 commit comments

Comments
 (0)