Index: clang/lib/Analysis/UnsafeBufferUsage.cpp =================================================================== --- clang/lib/Analysis/UnsafeBufferUsage.cpp +++ clang/lib/Analysis/UnsafeBufferUsage.cpp @@ -124,12 +124,18 @@ // Returns a matcher that matches any expression 'e' such that `innerMatcher` // matches 'e' and 'e' is in an Unspecified Lvalue Context. static auto isInUnspecifiedLvalueContext(internal::Matcher innerMatcher) { - auto isLvalueToRvalueCast = [](internal::Matcher M) { - return implicitCastExpr(hasCastKind(CastKind::CK_LValueToRValue), - castSubExpr(M)); - }; - //FIXME: add assignmentTo context... - return isLvalueToRvalueCast(innerMatcher); +// clang-format off + return + expr(eachOf( + implicitCastExpr( + hasCastKind(CastKind::CK_LValueToRValue), + castSubExpr(innerMatcher)), + binaryOperator( + hasAnyOperatorName("="), + hasLHS(innerMatcher) + ) + )); +// clang-format off } } // namespace clang::ast_matchers Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-assign-to-array-subscr-on-ptr.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-assign-to-array-subscr-on-ptr.cpp @@ -0,0 +1,16 @@ +// RUN: cp %s %t.cpp +// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fixit %t.cpp +// RUN: grep -v CHECK %t.cpp | FileCheck %s + +// TODO cases where we don't want fixits + +// The Fix-It for unsafe operation is trivially empty. +// In order to test that our machinery recognizes that we can test if the variable declaration gets a Fix-It. +// If the operation wasn't handled propertly the declaration won't get Fix-It. +// By testing presence of the declaration Fix-It we indirectly test presence of the trivial Fix-It for its operations. +void test() { + int *p = new int[10]; + // CHECK: std::span p {new int[10], 10}; + p[5] = 1; + // CHECK: p[5] = 1; +}