From 6e2f18acc363e0484cb96327bbced7344d157642 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 20 Oct 2023 14:28:27 -0500 Subject: [PATCH 1/2] Remove special case MicrosoftCodeAnalysisVersionForBannedApiAnalyzersTests --- eng/Versions.props | 1 - ...alysis.BannedApiAnalyzers.UnitTests.csproj | 4 +- ...strictedInternalsVisibleToAnalyzerTests.cs | 47 ++++++++++++------- .../UnitTests/SymbolIsBannedAnalyzerTests.cs | 12 +++++ 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index 27a6d9ad2c..07043a53b1 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -64,7 +64,6 @@ 3.11.0 1.2.1 3.11.0 - 3.11.0 3.11.0 3.11.0 4.0.1 diff --git a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/Microsoft.CodeAnalysis.BannedApiAnalyzers.UnitTests.csproj b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/Microsoft.CodeAnalysis.BannedApiAnalyzers.UnitTests.csproj index 6381e3f419..3449f60633 100644 --- a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/Microsoft.CodeAnalysis.BannedApiAnalyzers.UnitTests.csproj +++ b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/Microsoft.CodeAnalysis.BannedApiAnalyzers.UnitTests.csproj @@ -4,9 +4,7 @@ $(NetCurrent) true true - - $(MicrosoftCodeAnalysisVersionForBannedApiAnalyzersTests) + $(MicrosoftCodeAnalysisVersionForTests) diff --git a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs index cf1373ebac..68fa91d5bd 100644 --- a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs +++ b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs @@ -1039,21 +1039,21 @@ public C1(object o) { } }"; var apiConsumerSource = @" -[{|#0:N1.C1|}(typeof({|#1:N1.C1|}))] +[{|#16:{|#0:N1.C1|}(typeof({|#1:N1.C1|}))|}] class C2 { - [{|#2:N1.C1|}(typeof({|#3:N1.C1|}))] + [{|#17:{|#2:N1.C1|}(typeof({|#3:N1.C1|}))|}] private readonly int field; - [{|#4:N1.C1|}(typeof({|#5:N1.C1|}))] - private int Property { [{|#6:N1.C1|}(typeof({|#7:N1.C1|}))] get; } + [{|#18:{|#4:N1.C1|}(typeof({|#5:N1.C1|}))|}] + private int Property { [{|#19:{|#6:N1.C1|}(typeof({|#7:N1.C1|}))|}] get; } - [{|#8:N1.C1|}(typeof({|#9:N1.C1|}))] + [{|#20:{|#8:N1.C1|}(typeof({|#9:N1.C1|}))|}] private event System.EventHandler X; - [{|#10:N1.C1|}(typeof({|#11:N1.C1|}))] - [return: {|#12:N1.C1|}(typeof({|#13:N1.C1|}))] - int M([{|#14:N1.C1|}(typeof({|#15:N1.C1|}))]object c) + [{|#21:{|#10:N1.C1|}(typeof({|#11:N1.C1|}))|}] + [return: {|#22:{|#12:N1.C1|}(typeof({|#13:N1.C1|}))|}] + int M([{|#23:{|#14:N1.C1|}(typeof({|#15:N1.C1|}))|}]object c) { return 0; } @@ -1075,7 +1075,15 @@ await VerifyCSharpAsync(apiProviderSource, apiConsumerSource, GetCSharpResultAt(12, "N1.C1", "N2"), GetCSharpResultAt(13, "N1.C1", "N2"), GetCSharpResultAt(14, "N1.C1", "N2"), - GetCSharpResultAt(15, "N1.C1", "N2") + GetCSharpResultAt(15, "N1.C1", "N2"), + GetCSharpResultAt(16, "N1.C1.C1", "N2"), + GetCSharpResultAt(17, "N1.C1.C1", "N2"), + GetCSharpResultAt(18, "N1.C1.C1", "N2"), + GetCSharpResultAt(19, "N1.C1.C1", "N2"), + GetCSharpResultAt(20, "N1.C1.C1", "N2"), + GetCSharpResultAt(21, "N1.C1.C1", "N2"), + GetCSharpResultAt(22, "N1.C1.C1", "N2"), + GetCSharpResultAt(23, "N1.C1.C1", "N2") ); } @@ -1095,19 +1103,19 @@ End Class End Namespace"; var apiConsumerSource = @" -<{|#0:N1.C1|}(GetType({|#1:N1.C1|}))> +<{|#14:{|#0:N1.C1|}(GetType({|#1:N1.C1|}))|}> Class C2 - <{|#2:N1.C1|}(GetType({|#3:N1.C1|}))> + <{|#15:{|#2:N1.C1|}(GetType({|#3:N1.C1|}))|}> Private ReadOnly field As Integer - <{|#4:N1.C1|}(GetType({|#5:N1.C1|}))> + <{|#16:{|#4:N1.C1|}(GetType({|#5:N1.C1|}))|}> Private ReadOnly Property [Property] As Integer - <{|#6:N1.C1|}(GetType({|#7:N1.C1|}))> + <{|#17:{|#6:N1.C1|}(GetType({|#7:N1.C1|}))|}> Private Event X As System.EventHandler - <{|#8:N1.C1|}(GetType({|#9:N1.C1|}))> - Private Function M(<{|#10:N1.C1|}(GetType({|#11:N1.C1|}))> ByVal c As Object) As <{|#12:N1.C1|}(GetType({|#13:N1.C1|}))> Integer + <{|#18:{|#8:N1.C1|}(GetType({|#9:N1.C1|}))|}> + Private Function M(<{|#19:{|#10:N1.C1|}(GetType({|#11:N1.C1|}))|}> ByVal c As Object) As <{|#20:{|#12:N1.C1|}(GetType({|#13:N1.C1|}))|}> Integer Return 0 End Function End Class"; @@ -1126,7 +1134,14 @@ await VerifyBasicAsync(apiProviderSource, apiConsumerSource, GetBasicResultAt(10, "N1.C1", "N2"), GetBasicResultAt(11, "N1.C1", "N2"), GetBasicResultAt(12, "N1.C1", "N2"), - GetBasicResultAt(13, "N1.C1", "N2") + GetBasicResultAt(13, "N1.C1", "N2"), + GetBasicResultAt(14, "N1.C1.New", "N2"), + GetBasicResultAt(15, "N1.C1.New", "N2"), + GetBasicResultAt(16, "N1.C1.New", "N2"), + GetBasicResultAt(17, "N1.C1.New", "N2"), + GetBasicResultAt(18, "N1.C1.New", "N2"), + GetBasicResultAt(19, "N1.C1.New", "N2"), + GetBasicResultAt(20, "N1.C1.New", "N2") ); } diff --git a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/SymbolIsBannedAnalyzerTests.cs b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/SymbolIsBannedAnalyzerTests.cs index d667f00e16..65362884b8 100644 --- a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/SymbolIsBannedAnalyzerTests.cs +++ b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/SymbolIsBannedAnalyzerTests.cs @@ -966,6 +966,7 @@ class D : C { } var bannedText = @"T:BannedAttribute"; await VerifyCSharpAnalyzerAsync(source, bannedText, + GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -987,6 +988,7 @@ class C var bannedText = @"T:BannedAttribute"; await VerifyCSharpAnalyzerAsync(source, bannedText, + GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -1005,6 +1007,7 @@ class BannedAttribute : Attribute { } var bannedText = @"T:BannedAttribute"; await VerifyCSharpAnalyzerAsync(source, bannedText, + GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -1023,6 +1026,7 @@ class BannedAttribute : Attribute { } var bannedText = @"T:BannedAttribute"; await VerifyCSharpAnalyzerAsync(source, bannedText, + GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -1093,11 +1097,13 @@ class D {} await VerifyCSharpAnalyzerAsync( source, bannedText1, + GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute.BannedAttribute()", ""), GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute.BannedAttribute()", "")); await VerifyCSharpAnalyzerAsync( source, bannedText2, + GetCSharpResultAt(1, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute.BannedAttribute(int)", ""), GetCSharpResultAt(1, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute.BannedAttribute(int)", "")); } @@ -1812,6 +1818,7 @@ End Class var bannedText = @"T:BannedAttribute"; await VerifyBasicAnalyzerAsync(source, bannedText, + GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -1834,6 +1841,7 @@ End Class var bannedText = @"T:BannedAttribute"; await VerifyBasicAnalyzerAsync(source, bannedText, + GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -1854,6 +1862,7 @@ End Class var bannedText = @"T:BannedAttribute"; await VerifyBasicAnalyzerAsync(source, bannedText, + GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -1874,6 +1883,7 @@ End Class var bannedText = @"T:BannedAttribute"; await VerifyBasicAnalyzerAsync(source, bannedText, + GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""), GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", "")); } @@ -1940,11 +1950,13 @@ End Class await VerifyBasicAnalyzerAsync( source, bannedText1, + GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "Public Sub New()", ""), GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "Public Sub New()", "")); await VerifyBasicAnalyzerAsync( source, bannedText2, + GetBasicResultAt(1, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "Public Sub New(Banned As Integer)", ""), GetBasicResultAt(1, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "Public Sub New(Banned As Integer)", "")); } From 1f8488e7d293d1e0fc2461af5a5c924d395d7792 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 20 Oct 2023 14:38:07 -0500 Subject: [PATCH 2/2] Remove special case MicrosoftCodeAnalysisVersionForPerfSensitiveAnalyzersTests --- eng/Versions.props | 1 - .../Core/ExplicitAllocationAnalyzer.cs | 7 +++++++ ...Analysis.PerformanceSensitiveAnalyzers.UnitTests.csproj | 4 +--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index 07043a53b1..05e93a2192 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -65,7 +65,6 @@ 1.2.1 3.11.0 3.11.0 - 3.11.0 4.0.1 3.11.0 3.11.0 diff --git a/src/PerformanceSensitiveAnalyzers/Core/ExplicitAllocationAnalyzer.cs b/src/PerformanceSensitiveAnalyzers/Core/ExplicitAllocationAnalyzer.cs index 534581bd13..99c62aa68f 100644 --- a/src/PerformanceSensitiveAnalyzers/Core/ExplicitAllocationAnalyzer.cs +++ b/src/PerformanceSensitiveAnalyzers/Core/ExplicitAllocationAnalyzer.cs @@ -4,6 +4,7 @@ using System.Collections.Immutable; using Analyzer.Utilities; using Analyzer.Utilities.Extensions; +using Analyzer.Utilities.Lightup; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Operations; @@ -89,6 +90,12 @@ protected override void AnalyzeNode(OperationAnalysisContext context, in Perform if (context.Operation is IObjectCreationOperation or ITypeParameterObjectCreationOperation) { + if (context.Operation.Parent?.Kind == OperationKindEx.Attribute) + { + // Don't report attribute usage as creating a new instance + return; + } + if (context.Operation.Type?.IsReferenceType == true) { context.ReportDiagnostic(context.Operation.CreateDiagnostic(ObjectCreationRule, EmptyMessageArgs)); diff --git a/src/PerformanceSensitiveAnalyzers/UnitTests/Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers.UnitTests.csproj b/src/PerformanceSensitiveAnalyzers/UnitTests/Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers.UnitTests.csproj index a99d6341b6..b4bbfc860e 100644 --- a/src/PerformanceSensitiveAnalyzers/UnitTests/Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers.UnitTests.csproj +++ b/src/PerformanceSensitiveAnalyzers/UnitTests/Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers.UnitTests.csproj @@ -9,9 +9,7 @@ $(NoWarn);CA2007 true - - $(MicrosoftCodeAnalysisVersionForPerfSensitiveAnalyzersTests) + $(MicrosoftCodeAnalysisVersionForTests)