Skip to content

Don't trigger naming style violation for prefixed numbers. #48306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
5 commits merged into from
Oct 15, 2020
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
31 changes: 31 additions & 0 deletions src/Analyzers/CSharp/Tests/NamingStyles/NamingStylesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,5 +1334,36 @@ await TestInRegularAndScriptAsync(
@"public record Foo(int [|MyInt|]);",
options: s_options.MergeStyles(s_options.PropertyNamesArePascalCase, s_options.ParameterNamesAreCamelCaseWithPUnderscorePrefix));
}

[Theory]
[InlineData("_")]
[InlineData("_1")]
[InlineData("_123")]
public async Task TestDiscardParameterAsync(string identifier)
{
await TestMissingInRegularAndScriptAsync(
$@"class C
{{
void M(int [|{identifier}|])
{{
}}
}}", new TestParameters(options: s_options.ParameterNamesAreCamelCase));
}

[Theory]
[InlineData("_")]
[InlineData("_1")]
[InlineData("_123")]
public async Task TestDiscardLocalAsync(string identifier)
{
await TestMissingInRegularAndScriptAsync(
$@"class C
{{
void M()
{{
int [|{identifier}|] = 0;
}}
}}", new TestParameters(options: s_options.LocalNamesAreCamelCase));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.NamingStyles;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Utilities;

Expand Down Expand Up @@ -116,6 +117,11 @@ void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
return null;
}

if (symbol.IsSymbolWithSpecialDiscardName())
{
return null;
}

var namingPreferences = GetNamingStylePreferences(compilation, symbol, options, cancellationToken);
if (namingPreferences == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ bool ShouldReportUnusedValueDiagnostic(
if (_options.UnusedValueAssignmentSeverity == ReportDiagnostic.Suppress ||
symbol.GetSymbolType().IsErrorType() ||
(symbol.IsStatic && symbol.Kind == SymbolKind.Local) ||
IsSymbolWithSpecialDiscardName(symbol))
symbol.IsSymbolWithSpecialDiscardName())
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private bool IsUnusedParameterCandidate(IParameterSymbol parameter)
// without disabling the diagnostic completely.
// We ignore parameter names that start with an underscore and are optionally followed by an integer,
// such as '_', '_1', '_2', etc.
if (IsSymbolWithSpecialDiscardName(parameter))
if (parameter.IsSymbolWithSpecialDiscardName())
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,5 @@ public static bool GetIsRemovableAssignmentDiagnostic(Diagnostic diagnostic)
Debug.Assert(TryGetUnusedValuePreference(diagnostic, out _));
return diagnostic.Properties.ContainsKey(IsRemovableAssignmentKey);
}

/// <summary>
/// Returns true for symbols whose name starts with an underscore and
/// are optionally followed by an integer, such as '_', '_1', '_2', etc.
/// These are treated as special discard symbol names.
/// </summary>
private static bool IsSymbolWithSpecialDiscardName(ISymbol symbol)
=> symbol.Name.StartsWith("_") &&
(symbol.Name.Length == 1 || uint.TryParse(symbol.Name.Substring(1), out _));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -740,5 +740,14 @@ public static bool IsKind<TSymbol>(this ISymbol symbol, SymbolKind kind, [NotNul
result = (TSymbol)symbol;
return true;
}

/// <summary>
/// Returns true for symbols whose name starts with an underscore and
/// are optionally followed by an integer, such as '_', '_1', '_2', etc.
/// These are treated as special discard symbol names.
/// </summary>
public static bool IsSymbolWithSpecialDiscardName(this ISymbol symbol)
=> symbol.Name.StartsWith("_") &&
(symbol.Name.Length == 1 || uint.TryParse(symbol.Name.Substring(1), out _));
}
}