Skip to content

Commit

Permalink
Fix MA0099 for default values in attributes when using Roslyn4.4 or b…
Browse files Browse the repository at this point in the history
…elow (#526)
  • Loading branch information
meziantou committed May 11, 2023
1 parent 3b518d8 commit da94855
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.5.0" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);ROSLYN4_4;ROSLYN_4_2_OR_GREATER;ROSLYN_4_4_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);ROSLYN4_4;ROSLYN_4_2_OR_GREATER;ROSLYN_4_4_OR_GREATER;ROSLYN_4_5_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);CSHARP9_OR_GREATER;CSHARP10_OR_GREATER;CSHARP11_OR_GREATER</DefineConstants>
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ private static void AnalyzeConversion(OperationAnalysisContext context)
if (operation.Parent is IArgumentOperation { IsImplicit: true })
return;

#if !ROSLYN4_5_OR_GREATER
if (operation.Syntax.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.Attribute))
return;
#endif

if (operation.ConstantValue is { HasValue: true, Value: not null and var value } && IsZero(enumType, value))
{
context.ReportDiagnostic(s_rule, operation, operation.Type.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,38 @@ void A(MyEnum a = MyEnum.B)
A();
}
}
""")
.ValidateAsync();
}

[Fact]
[Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/525")]
public async Task ImplicitParameterInAttribute()
{
await CreateProjectBuilder()
.WithSourceCode($$"""
using System;

public enum MyEnum
{
None = 0,
Some = 1,
}

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class MyAttribute : Attribute
{
public MyAttribute(MyEnum bar = MyEnum.None) { }
}

[MyAttribute]
[MyAttribute(MyEnum.None)]
[MyAttribute(MyEnum.Some)]
[MyAttribute([|0|])]
public class MyClass
{
public MyClass(MyEnum foo = MyEnum.None) { }
}
""")
.ValidateAsync();
}
Expand Down

0 comments on commit da94855

Please sign in to comment.