Skip to content

Commit

Permalink
Allow MaxLength magic number assignment for MEN010
Browse files Browse the repository at this point in the history
Also, allow FromMicroseconds and FromMinutes TimeSpan methods
  • Loading branch information
menees committed Mar 27, 2024
1 parent a401dd6 commit 8c168ea
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<!-- Make the assembly, file, and NuGet package versions the same. -->
<!-- NOTE: Change the version in Vsix\source.extension.vsixmanifest to match this! -->
<Version>3.0.12</Version>
<Version>3.0.13</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
6 changes: 6 additions & 0 deletions src/Menees.Analyzers/Men010AvoidMagicNumbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ private static bool InAllowedDeclarationContext(LiteralExpressionSyntax literalE
case SyntaxKind.RangeExpression:
allowed = literalExpression.Parent == ancestor;
break;

case SyntaxKind.SimpleAssignmentExpression:
allowed = ancestor is AssignmentExpressionSyntax assignment
&& assignment.Left is MemberAccessExpressionSyntax member
&& settings.IsAllowedNumericLiteralCaller(member.Name.Identifier.Text);
break;
}

return allowed;
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Analyzers/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal sealed class Settings
private readonly HashSet<string> allowedNumericLiterals = new(new[] { "0", "1", "2", "100" });
private readonly HashSet<string> allowedNumericCallerNames = new(new[]
{
"FromDays", "FromHours", "FromMilliseconds", "FromSeconds", "FromTicks"
"FromDays", "FromHours", "FromMicroseconds", "FromMilliseconds", "FromMinutes", "FromSeconds", "FromTicks", "MaxLength"
});
private readonly IEnumerable<Predicate<string>>? allowedNumericCallerRegexes;
private readonly Dictionary<string, string> preferredTerms = DefaultPreferredTerms;
Expand Down
3 changes: 3 additions & 0 deletions tests/Menees.Analyzers.Test/Men010UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public double GetRate()
return FridayValue;
}
DataColumn col = new(""Test"", typeof(string));
col.MaxLength = 20;
const int DefaultValue = 17;
return DefaultValue;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Menees.Analyzers.Test/Menees.Analyzers.Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<Literal>1</Literal>
<Literal>2</Literal>
<CallerName>FromDays</CallerName>
<CallerName>MaxLength</CallerName>
<CallerRegex>^From(Hours|Minutes)$</CallerRegex>
</AllowedNumericLiterals>

Expand Down

0 comments on commit 8c168ea

Please sign in to comment.