Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file contains commits that should be ignored by git blame
# Format: one commit SHA per line with optional comment
#
# To use this file, run:
# git config blame.ignoreRevsFile .git-blame-ignore-revs

# Format AvoidSingleUseOfLocalJsonSerializerOptions.cs (imports ordering)
39fa404
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
using System.Diagnostics.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Microsoft.NetCore.Analyzers.Performance
{
Expand Down Expand Up @@ -60,6 +60,12 @@ private static void OnCompilationStart(CompilationStartAnalysisContext context)
INamedTypeSymbol? typeSymbol = operation.Constructor?.ContainingType;
if (SymbolEqualityComparer.Default.Equals(typeSymbol, jsonSerializerOptionsSymbol))
{
// Don't report diagnostic for top-level statements as they only execute once
if (context.ContainingSymbol is IMethodSymbol method && method.IsTopLevelStatementsEntryPointMethod())
{
return;
}

if (IsCtorUsedAsArgumentForJsonSerializer(operation, jsonSerializerSymbol) ||
IsLocalUsedAsArgumentForJsonSerializerOnly(operation, jsonSerializerSymbol, jsonSerializerOptionsSymbol))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@

Rule ID | Missing Help Link | Title |
--------|-------------------|-------|
CA1873 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873> | Avoid potentially expensive logging |
CA1874 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1874> | Use 'Regex.IsMatch' |
CA1875 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1875> | Use 'Regex.Count' |
CA2023 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2023> | Invalid braces in message template |
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Expand Down Expand Up @@ -820,6 +821,81 @@ Shared Function Serialize(Of T)(values As T()) As String
End Function
End Class
""");

[Fact]
public Task CS_TopLevelStatements_UseNewOptionsAsArgument_NoWarn()
{
var test = new VerifyCS.Test
{
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources =
{
"""
using System.Text.Json;

string json = JsonSerializer.Serialize(new[] { 1, 2, 3 }, new JsonSerializerOptions { AllowTrailingCommas = true });
"""
}
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9
};
return test.RunAsync();
}

[Fact]
public Task CS_TopLevelStatements_UseNewLocalOptionsAsArgument_NoWarn()
{
var test = new VerifyCS.Test
{
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources =
{
"""
using System.Text.Json;

JsonSerializerOptions options = new()
{
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip
};

var output = JsonSerializer.Deserialize<int[]>("[1,2,3]", options);
"""
}
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9
};
return test.RunAsync();
}

[Fact]
public Task CS_TopLevelStatements_UseNewLocalOptionsAsArgument_Assignment_NoWarn()
{
var test = new VerifyCS.Test
{
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources =
{
"""
using System.Text.Json;

JsonSerializerOptions options;
options = new JsonSerializerOptions();

var output = JsonSerializer.Deserialize<int[]>("[1,2,3]", options);
"""
}
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9
};
return test.RunAsync();
}
#endregion
}
}
Loading