Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@
<MicrosoftCodeAnalysisForRoslynDiagnosticsAnalyzersVersion>3.11.0</MicrosoftCodeAnalysisForRoslynDiagnosticsAnalyzersVersion>
<MicrosoftCodeAnalysisVersionForPublicApiAnalyzers>1.2.1</MicrosoftCodeAnalysisVersionForPublicApiAnalyzers>
<MicrosoftCodeAnalysisVersionForBannedApiAnalyzers>3.11.0</MicrosoftCodeAnalysisVersionForBannedApiAnalyzers>
<MicrosoftCodeAnalysisVersionForBannedApiAnalyzersTests>3.11.0</MicrosoftCodeAnalysisVersionForBannedApiAnalyzersTests>
<MicrosoftCodeAnalysisVersionForPerfSensitiveAnalyzers>3.11.0</MicrosoftCodeAnalysisVersionForPerfSensitiveAnalyzers>
<MicrosoftCodeAnalysisVersionForPerfSensitiveAnalyzersTests>3.11.0</MicrosoftCodeAnalysisVersionForPerfSensitiveAnalyzersTests>
<MicrosoftCodeAnalysisVersionForResxSourceGenerators>4.0.1</MicrosoftCodeAnalysisVersionForResxSourceGenerators>
<MicrosoftCodeAnalysisVersionForNetAnalyzers>3.11.0</MicrosoftCodeAnalysisVersionForNetAnalyzers>
<MicrosoftCodeAnalysisVersionForTextAnalyzers>3.11.0</MicrosoftCodeAnalysisVersionForTextAnalyzers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
<TargetFramework>$(NetCurrent)</TargetFramework>
<ServerGarbageCollection>true</ServerGarbageCollection>
<ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>
<!-- These unit tests fail when run agains MicrosoftCodeAnalysisVersionForTests (4.6.0-final.1).
https://github.com/dotnet/roslyn-analyzers/issues/6672. -->
<MicrosoftCodeAnalysisVersion>$(MicrosoftCodeAnalysisVersionForBannedApiAnalyzersTests)</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisVersion>$(MicrosoftCodeAnalysisVersionForTests)</MicrosoftCodeAnalysisVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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")
);
}

Expand All @@ -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";
Expand All @@ -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")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""));
}

Expand All @@ -987,6 +988,7 @@ class C
var bannedText = @"T:BannedAttribute";

await VerifyCSharpAnalyzerAsync(source, bannedText,
GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""),
GetCSharpResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""));
}

Expand All @@ -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", ""));
}

Expand All @@ -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", ""));
}

Expand Down Expand Up @@ -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)", ""));
}

Expand Down Expand Up @@ -1812,6 +1818,7 @@ End Class
var bannedText = @"T:BannedAttribute";

await VerifyBasicAnalyzerAsync(source, bannedText,
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""),
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""));
}

Expand All @@ -1834,6 +1841,7 @@ End Class
var bannedText = @"T:BannedAttribute";

await VerifyBasicAnalyzerAsync(source, bannedText,
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""),
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""));
}

Expand All @@ -1854,6 +1862,7 @@ End Class
var bannedText = @"T:BannedAttribute";

await VerifyBasicAnalyzerAsync(source, bannedText,
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""),
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""));
}

Expand All @@ -1874,6 +1883,7 @@ End Class
var bannedText = @"T:BannedAttribute";

await VerifyBasicAnalyzerAsync(source, bannedText,
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""),
GetBasicResultAt(0, SymbolIsBannedAnalyzer.SymbolIsBannedRule, "BannedAttribute", ""));
}

Expand Down Expand Up @@ -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)", ""));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
<NoWarn>$(NoWarn);CA2007</NoWarn>

<ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>
<!-- These unit tests fail when run agains MicrosoftCodeAnalysisVersionForTests (4.6.0-final.1).
https://github.com/dotnet/roslyn-analyzers/issues/6672. -->
<MicrosoftCodeAnalysisVersion>$(MicrosoftCodeAnalysisVersionForPerfSensitiveAnalyzersTests)</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisVersion>$(MicrosoftCodeAnalysisVersionForTests)</MicrosoftCodeAnalysisVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
Expand Down