diff --git a/csharp/ql/src/Language Abuse/UselessCastToSelf.ql b/csharp/ql/src/Language Abuse/UselessCastToSelf.ql index d1a08e1db1c5..96f93131fa6a 100644 --- a/csharp/ql/src/Language Abuse/UselessCastToSelf.ql +++ b/csharp/ql/src/Language Abuse/UselessCastToSelf.ql @@ -19,5 +19,6 @@ where type = cast.getTargetType() and type = e.getType() and not type instanceof NullType and + not type instanceof UnknownType and not e.(ImplicitDelegateCreation).getArgument() instanceof AnonymousFunctionExpr select cast, "This cast is redundant because the expression already has type " + type + "." diff --git a/csharp/ql/src/change-notes/2026-08-20-harden-useless-cast-to-self.md b/csharp/ql/src/change-notes/2026-08-20-harden-useless-cast-to-self.md new file mode 100644 index 000000000000..bb5bb85b86e7 --- /dev/null +++ b/csharp/ql/src/change-notes/2026-08-20-harden-useless-cast-to-self.md @@ -0,0 +1,4 @@ +--- +category: fix +--- +* The query `cs/useless-cast-to-self` no longer reports casts when both the expression type and the cast target type are unknown, which can occur in `build-mode: none` databases.