Skip to content

Fix MA0139 false positive when the Serilog params array is passed as an array - #1607

Merged
meziantou merged 1 commit into
mainfrom
fix/ma0139-serilog-params-array
Sep 23, 2026
Merged

meziantou merged 1 commit into
mainfrom
fix/ma0139-serilog-params-array

Conversation

@meziantou

Copy link
Copy Markdown
Owner

What

For the Serilog methods (Log.Information(string messageTemplate, params object?[]? propertyValues), ...), the values were read from the params array only when the call used the expanded form. When the array was passed directly (normal form), the call fell through to the fallback that uses each argument as a value, so the whole object[] was checked against the first placeholder:

// LoggerParameterTypes.txt: Prop;System.Int32
Log.Information("{Prop}", new object[] { 1 });
// MA0139: Log parameter 'Prop' must be of type 'global::System.Int32' but is of type 'global::System.Object[]'

Fix

When the argument after the template is bound to the params parameter, the values are the elements of the array creation, whether the call is in the expanded or in the normal form (implicit conversions are unwrapped). When the array is not created in the call (a variable, a method call, null...), its values are unknown, so no value is checked and only the placeholder names are validated, as when fewer values than placeholders are passed.

The generic overloads (Information<T>(string, T)) are unchanged: when they are selected, such as for new string[] { "" }, the array is a single property value.

Tests

Added SeriLog_Log_Information_ParamsArrayInNormalForm (valid and invalid element in new object[] { ... }) and SeriLog_Log_Information_ParamsArrayVariable. Both failed before the fix.

Ran the LoggerParameterTypeAnalyzer tests (MA0124 and MA0139) on roslyn5.9 and roslyn4.8 (63/63 passing on both).

@meziantou
meziantou marked this pull request as ready for review September 23, 2026 04:35
@meziantou
meziantou merged commit 52ec38e into main Sep 23, 2026
14 checks passed
@meziantou
meziantou deleted the fix/ma0139-serilog-params-array branch September 23, 2026 04:35
This was referenced Sep 23, 2026
This was referenced Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant