Skip to content

Commit

Permalink
Do not report hidden ARGP0005 for an error type, which looks like a…
Browse files Browse the repository at this point in the history
… `ParseResult`
  • Loading branch information
DoctorKrolic committed Jun 9, 2024
1 parent c0b5578 commit 7e5c502
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,16 @@ private static void AnalyzeParserSignature(SymbolAnalysisContext context, KnownT
if (returnType is not INamedTypeSymbol { TypeArguments: [var optionsType] } namedReturnType ||
!namedReturnType.ConstructedFrom.Equals(knownTypes.ParseResultOfTType, SymbolEqualityComparer.Default))
{
context.ReportDiagnostic(
Diagnostic.Create(
DiagnosticDescriptors.ReturnTypeMustBeParseResult,
returnTypeSyntax.GetLocation(),
effectiveSeverity: returnType.TypeKind == TypeKind.Error ? DiagnosticSeverity.Hidden : DiagnosticDescriptors.ReturnTypeMustBeParseResult.DefaultSeverity,
additionalLocations: null,
properties: null));
if (returnType.TypeKind != TypeKind.Error || returnType.Name != "ParseResult")
{
context.ReportDiagnostic(
Diagnostic.Create(
DiagnosticDescriptors.ReturnTypeMustBeParseResult,
returnTypeSyntax.GetLocation(),
effectiveSeverity: returnType.TypeKind == TypeKind.Error ? DiagnosticSeverity.Hidden : DiagnosticDescriptors.ReturnTypeMustBeParseResult.DefaultSeverity,
additionalLocations: null,
properties: null));
}
}
else if (optionsType is not INamedTypeSymbol { SpecialType: SpecialType.None, TypeKind: TypeKind.Class or TypeKind.Struct } namedOptionsType ||
!namedOptionsType.Constructors.Any(static c => c.DeclaredAccessibility >= Accessibility.Internal && c.Parameters.IsEmpty))
Expand Down
19 changes: 19 additions & 0 deletions tests/ArgumentParsing.Tests.Unit/ParserSignatureAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,25 @@ await VerifyAnalyzerWithCodeFixAsync<WrapReturnTypeIntoParseResultCodeFixProvide
]);
}

[Theory]
[InlineData("ParseResult")]
[InlineData("ParseResult<string>")]
[InlineData("ParseResult<{|CS0246:ErrorType|}>")]
public async Task InvalidReturnType_ErrorType_LooksLikeParseResult(string typeNameMarkup)
{
var source = $$"""
using ArgumentParsing;
partial class C
{
[GeneratedArgumentParser]
public static partial {|CS0246:{{typeNameMarkup}}|} {|CS8795:ParseArguments|}(string[] args);
}
""";

await VerifyAnalyzerAsync(source, addCommonUsings: false);
}

[Theory]
[InlineData("int")]
[InlineData("string")]
Expand Down
31 changes: 22 additions & 9 deletions tests/ArgumentParsing.Tests.Unit/Utilities/AnalyzerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@ namespace ArgumentParsing.Tests.Unit.Utilities;
public abstract class AnalyzerTestBase<TAnalyzer>
where TAnalyzer : DiagnosticAnalyzer, new()
{
protected static Task VerifyAnalyzerAsync(string source, LanguageVersion languageVersion = LanguageVersion.Latest, ReferenceAssemblies referenceAssemblies = null, CompilerDiagnostics compilerDiagnostics = CompilerDiagnostics.Errors)
=> VerifyAnalyzerAsync(source, [], languageVersion, referenceAssemblies, compilerDiagnostics);
protected static Task VerifyAnalyzerAsync(
string source,
LanguageVersion languageVersion = LanguageVersion.Latest,
ReferenceAssemblies referenceAssemblies = null,
CompilerDiagnostics compilerDiagnostics = CompilerDiagnostics.Errors,
bool addCommonUsings = true)
=> VerifyAnalyzerAsync(source, [], languageVersion, referenceAssemblies, compilerDiagnostics, addCommonUsings);

protected static Task VerifyAnalyzerAsync(string source, DiagnosticResult[] diagnostics, LanguageVersion languageVersion = LanguageVersion.Latest, ReferenceAssemblies referenceAssemblies = null, CompilerDiagnostics compilerDiagnostics = CompilerDiagnostics.Errors)
=> VerifyAnalyzerWithCodeFixAsync<EmptyCodeFixProvider>(source, fixedSource: null, diagnostics, languageVersion, referenceAssemblies, compilerDiagnostics);
protected static Task VerifyAnalyzerAsync(
string source,
DiagnosticResult[] diagnostics,
LanguageVersion languageVersion = LanguageVersion.Latest,
ReferenceAssemblies referenceAssemblies = null,
CompilerDiagnostics compilerDiagnostics = CompilerDiagnostics.Errors,
bool addCommonUsings = true)
=> VerifyAnalyzerWithCodeFixAsync<EmptyCodeFixProvider>(source, fixedSource: null, diagnostics, languageVersion, referenceAssemblies, compilerDiagnostics, addCommonUsings: addCommonUsings);

protected static Task VerifyAnalyzerWithCodeFixAsync<TCodeFix>(
string source,
string fixedSource,
LanguageVersion languageVersion = LanguageVersion.Latest,
ReferenceAssemblies referenceAssemblies = null,
CompilerDiagnostics compilerDiagnostics = CompilerDiagnostics.Errors,
int codeActionIndex = 0)
int codeActionIndex = 0,
bool addCommonUsings = true)
where TCodeFix : CodeFixProvider, new()
{
return VerifyAnalyzerWithCodeFixAsync<TCodeFix>(source, fixedSource, [], languageVersion, referenceAssemblies, compilerDiagnostics, codeActionIndex);
return VerifyAnalyzerWithCodeFixAsync<TCodeFix>(source, fixedSource, [], languageVersion, referenceAssemblies, compilerDiagnostics, codeActionIndex, addCommonUsings);
}

protected static async Task VerifyAnalyzerWithCodeFixAsync<TCodeFix>(
Expand All @@ -35,7 +47,8 @@ protected static async Task VerifyAnalyzerWithCodeFixAsync<TCodeFix>(
LanguageVersion languageVersion = LanguageVersion.Latest,
ReferenceAssemblies referenceAssemblies = null,
CompilerDiagnostics compilerDiagnostics = CompilerDiagnostics.Errors,
int codeActionIndex = 0)
int codeActionIndex = 0,
bool addCommonUsings = true)
where TCodeFix : CodeFixProvider, new()
{
var usings = """
Expand Down Expand Up @@ -78,7 +91,7 @@ class EmptyOptions { }
ReferenceAssemblies = referenceAssemblies ?? ReferenceAssemblies.Net.Net80,
};

if (languageVersion >= LanguageVersion.CSharp10)
if (addCommonUsings && languageVersion >= LanguageVersion.CSharp10)
{
test.TestState.Sources.Add(usings);
}
Expand All @@ -89,7 +102,7 @@ class EmptyOptions { }
{
test.FixedState.Sources.Add(fixedSource);
test.FixedState.Sources.Add(emptyOptions);
if (languageVersion >= LanguageVersion.CSharp10)
if (addCommonUsings && languageVersion >= LanguageVersion.CSharp10)
{
test.FixedState.Sources.Add(usings);
}
Expand Down

0 comments on commit 7e5c502

Please sign in to comment.