Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -371,6 +371,8 @@ ``#pragma clang|GCC diagnostic push|pop`` directive. (`#13920: `_) - Clang now does not try to analyze cast validity on variables with dependent alignment (`#63007: `_). +- ``-Wformat`` cast fix-its will now suggest ``static_cast`` instead of C-style casts + for C++ code. Bug Fixes in This Version ------------------------- Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -10822,9 +10822,9 @@ // if necessary). SmallString<16> CastBuf; llvm::raw_svector_ostream CastFix(CastBuf); - CastFix << "("; + CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "("); IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); - CastFix << ")"; + CastFix << (S.LangOpts.CPlusPlus ? ">" : ")"); SmallVector Hints; if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly) @@ -10835,7 +10835,7 @@ SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); - } else if (!requiresParensToAddCast(E)) { + } else if (!requiresParensToAddCast(E) && !S.LangOpts.CPlusPlus) { // If the expression has high enough precedence, // just write the C-style cast. Hints.push_back( Index: clang/test/FixIt/format.mm =================================================================== --- clang/test/FixIt/format.mm +++ clang/test/FixIt/format.mm @@ -9,22 +9,22 @@ const wchar_t wchar_data = L'a'; NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} - // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"(unsigned short)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast( NSLog(@"%C", 0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}} // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d" - // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unsigned short)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"static_cast( typedef unsigned short unichar; NSLog(@"%C", wchar_data); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}} - // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"(unichar)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast( NSLog(@"%C", 0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'int'}} // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d" - // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"static_cast( NSLog(@"%C", 0.0); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'double'}} // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%f" - // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)" + // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"static_cast( }