Skip to content

[clang-tidy] fix match for binaryOperator in ExprMutationAnalyzer for misc-const-correctness #70559

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ Changes in existing checks

- Improved :doc:`misc-const-correctness
<clang-tidy/checks/misc/const-correctness>` check to avoid false positive when
using pointer to member function.
using pointer to member function. Additionally, the check no longer emits
a diagnostic when a variable that is not type-dependent is an operand of a
type-dependent binary operator.

- Improved :doc:`misc-include-cleaner
<clang-tidy/checks/misc/include-cleaner>` check by adding option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ void instantiate_template_cases() {
type_dependent_variables<int>();
type_dependent_variables<float>();
}

namespace gh57297{
// The expression to check may not be the dependent operand in a dependent
// operator.

// Explicitly not declaring a (templated) stream operator
// so the `<<` is a `binaryOperator` with a dependent type.
struct Stream { };
template <typename T> void f() { T t; Stream x; x << t; }
} // namespace gh57297
6 changes: 3 additions & 3 deletions clang/lib/Analysis/ExprMutationAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ const Stmt *ExprMutationAnalyzer::findDirectMutation(const Expr *Exp) {
// resolved and modelled as `binaryOperator` on a dependent type.
// Such instances are considered a modification, because they can modify
// in different instantiations of the template.
binaryOperator(hasEitherOperand(
allOf(ignoringImpCasts(canResolveToExpr(equalsNode(Exp))),
isTypeDependent()))),
binaryOperator(
hasEitherOperand(ignoringImpCasts(canResolveToExpr(equalsNode(Exp)))),
isTypeDependent()),
// Within class templates and member functions the member expression might
// not be resolved. In that case, the `callExpr` is considered to be a
// modification.
Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,22 @@ TEST(ExprMutationAnalyzerTest, UnresolvedOperator) {
EXPECT_TRUE(isMutated(Results, AST.get()));
}

TEST(ExprMutationAnalyzerTest, DependentOperatorWithNonDependentOperand) {
// gh57297
// The expression to check may not be the dependent operand in a dependent
// operator.

// Explicitly not declaring a (templated) stream operator
// so the `<<` is a `binaryOperator` with a dependent type.
const auto AST = buildASTFromCodeWithArgs(
"struct Stream { };"
"template <typename T> void f() { T t; Stream x; x << t; }",
{"-fno-delayed-template-parsing"});
const auto Results =
match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x << t"));
}

// Section: expression as call argument

TEST(ExprMutationAnalyzerTest, ByValueArgument) {
Expand Down