Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1f4234a
Add ConstantExpectedAnalyzer
wzchua Feb 25, 2022
f39fc60
Fix floating point string culture issue
wzchua Feb 25, 2022
74d9533
Update tests
wzchua Feb 25, 2022
d13a77e
Fix typos
wzchua Feb 25, 2022
8b3d43e
Rework test cases
wzchua Feb 25, 2022
d4fe364
run pack command
wzchua Feb 25, 2022
5f8922f
Reorganize and combine some rules.
wzchua Feb 25, 2022
7b883e0
Apply suggestions from code review
wzchua Feb 25, 2022
f784732
Merge branch 'main' into new-analyzer/constant-expected
wzchua May 28, 2022
d12e2fb
Apply suggestion from feedback
wzchua May 29, 2022
9d4c1a3
Merge branch 'main' into new-analyzer/constant-expected
wzchua Jul 16, 2022
db80589
Apply suggestions from code review
wzchua Jul 16, 2022
68e6e28
Apply suggestions from code review
wzchua Jul 16, 2022
52f5e3f
rebuild
wzchua Jul 16, 2022
f991607
Use const variable for Min Max literal
wzchua Jul 16, 2022
eab469f
Drop nint nuint support from ConstantExpected
wzchua Jul 16, 2022
5e1b4c3
Add early exit if symbol for attribute is not found
wzchua Jul 16, 2022
e7ac792
Add check for enums
wzchua Jul 23, 2022
1196110
Merge branch 'main' into new-analyzer/constant-expected
wzchua Aug 27, 2022
65de245
Fix RS1032
wzchua Aug 27, 2022
04ad5b1
Fix IDE0200: Lambda expression can be removed
wzchua Aug 27, 2022
011d9eb
Rework attribute symbol matching
wzchua Aug 27, 2022
ec3f1ae
Update diagnostic Ids
wzchua Aug 27, 2022
59fdd58
Update test to use ReferenceAssemblies.Net70
wzchua Aug 27, 2022
865974c
Add performance tests
wzchua Aug 27, 2022
c87ee13
Apply suggestions from code review
buyaa-n Sep 13, 2022
5f07e36
Update localized messages
buyaa-n Sep 13, 2022
7c8b93b
Merge branch 'main' into new-analyzer/constant-expected
wzchua Nov 12, 2022
266fba8
Run pack
wzchua Nov 12, 2022
86dbee4
Merge branch 'main' into new-analyzer/constant-expected
buyaa-n Nov 16, 2022
774008b
Apply suggestions from code review
buyaa-n Nov 16, 2022
ca89072
Fix generated file
buyaa-n Nov 16, 2022
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
Prev Previous commit
Next Next commit
Add early exit if symbol for attribute is not found
  • Loading branch information
wzchua committed Jul 16, 2022
commit 5e1b4c35baa6e90c8519f25e98d366969764d2da
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public override void Initialize(AnalysisContext context)

private void OnCompilationStart(CompilationStartAnalysisContext context)
{
if (!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemDiagnosticsCodeAnalysisConstantExpectedAttribute, out _))
{
return;
}
context.RegisterOperationAction(OnInvocation, OperationKind.Invocation);
context.RegisterSymbolAction(context => OnMethodSymbol(context), SymbolKind.Method);
RegisterAttributeSyntax(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,53 @@ await TestCSAsync(csInput,
.WithLocation(2));
}

[Theory]
[InlineData("char")]
[InlineData("sbyte")]
[InlineData("short")]
[InlineData("int")]
[InlineData("long")]
[InlineData("byte")]
[InlineData("ushort")]
[InlineData("uint")]
[InlineData("ulong")]
[InlineData("float")]
[InlineData("double")]
[InlineData("bool")]
[InlineData("string")]
public static async Task TestMissingConstantExpectedSupportedComplex2TypesAsync(string type)
{
string csInput = @$"
using System;
using Similar;
#nullable enable

public class Test
{{
public interface ITest<T>
{{
T Method(T operand1, [ConstantExpected] T operand2);
}}
public interface ITest2<T>
{{
T Method(T operand1, [ConstantExpected] T operand2);
}}
public abstract class AbstractTest<T>
{{
public abstract T Method2(T operand1, [ConstantExpected] T operand2);
}}

public class Generic : AbstractTest<{type}>, ITest<{type}>, ITest2<{type}>
{{
public {type} Method({type} operand1, {type} operand2) => throw new NotImplementedException();
{type} ITest2<{type}>.Method({type} operand1, {type} operand2) => throw new NotImplementedException();
public override {type} Method2({type} operand1, {type} operand2) => throw new NotImplementedException();
}}
}}
";
await TestCSMissingAttributeAsync(csInput);
}

[Fact]
public static async Task TestConstantExpectedSupportedComplex3TypesAsync()
{
Expand Down Expand Up @@ -691,6 +738,31 @@ private static async Task TestCSAsync(string source, params DiagnosticResult[] d

private static readonly string s_attributeSource = @"#nullable enable
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public sealed class ConstantExpectedAttribute : Attribute
{
public object? Min { get; set; }
public object? Max { get; set; }
}
}";

private static async Task TestCSMissingAttributeAsync(string source, params DiagnosticResult[] diagnosticResults)
{
var test = new VerifyCS.Test
{
TestCode = source,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.Preview,
};
test.TestState.Sources.Add(s_similarAttributeSource);
test.ExpectedDiagnostics.AddRange(diagnosticResults);
await test.RunAsync();
}

private static readonly string s_similarAttributeSource = @"#nullable enable
using System;
namespace Similar
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public sealed class ConstantExpectedAttribute : Attribute
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Compiler/WellKnownTypeNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ internal static class WellKnownTypeNames
public const string SystemDateTimeOffset = "System.DateTimeOffset";
public const string SystemDecimal = "System.Decimal";
public const string SystemDiagnosticContractsContract = "System.Diagnostics.Contracts.Contract";
public const string SystemDiagnosticsCodeAnalysisConstantExpectedAttribute = "System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute";
public const string SystemDiagnosticsCodeAnalysisNotNullAttribute = "System.Diagnostics.CodeAnalysis.NotNullAttribute";
public const string SystemDiagnosticsConditionalAttribute = "System.Diagnostics.ConditionalAttribute";
public const string SystemDiagnosticsContractsPureAttribute = "System.Diagnostics.Contracts.PureAttribute";
Expand Down