[clang-tools-extra] [clang-tidy] Fix bugprone-misplaced-widening-cast false positive on bitfield assignments (PR #197554)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 11 23:57:20 PDT 2026


================
@@ -42,7 +42,16 @@ void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(varDecl(hasInitializer(Cast)), this);
   Finder->addMatcher(returnStmt(hasReturnValue(Cast)), this);
   Finder->addMatcher(callExpr(hasAnyArgument(Cast)), this);
-  Finder->addMatcher(binaryOperator(hasOperatorName("="), hasRHS(Cast)), this);
+  // When assigning to a bit field, bind the FieldDecl so check() can use the
+  // actual bit width instead of the declared type width. optionally() ensures
+  // the matcher still fires for assignments to a field which is not a bit field
+  // (binding is absent).
----------------
vbvictor wrote:

```suggestion
  // When assigning to a bit field, bind the FieldDecl so check() can use the
  // actual bit width instead of the declared type width.
```
Second part of comment is excessive, it is just description of `optional()`

https://github.com/llvm/llvm-project/pull/197554


More information about the cfe-commits mailing list