Skip to content
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

RS0046: Avoid Opt suffix in nullable-enabled code #3352

Merged
merged 20 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Analyzer.Utilities.Extensions;
using Analyzer.Utilities.Lightup;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Diagnostics.Analyzers;

namespace Roslyn.Diagnostics.CSharp.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class CSharpAvoidOptSuffixForNullableEnableCode : AvoidOptSuffixForNullableEnableCode
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
{
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

context.RegisterSyntaxNodeAction(context =>
{
var parameter = (ParameterSyntax)context.Node;
Evangelink marked this conversation as resolved.
Show resolved Hide resolved

if (parameter.Identifier.Text.EndsWith("Opt", System.StringComparison.Ordinal) &&
context.SemanticModel.GetNullableContext(parameter.SpanStart).AnnotationsEnabled())
{
context.ReportDiagnostic(parameter.Identifier.CreateDiagnostic(Rule));
}
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
}, SyntaxKind.Parameter);
Evangelink marked this conversation as resolved.
Show resolved Hide resolved

context.RegisterSyntaxNodeAction(context =>
{
var fieldDeclaration = (FieldDeclarationSyntax)context.Node;

foreach (var variable in fieldDeclaration.Declaration.Variables)
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
{
if (variable.Identifier.Text.EndsWith("Opt", System.StringComparison.Ordinal) &&
context.SemanticModel.GetNullableContext(variable.SpanStart).AnnotationsEnabled())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory it's possible to have some parts of a field declaration after the #nullable enable but I don't think that would happen in Roslyn so we might want to check the context at the field declaration level rather than each variable.

{
context.ReportDiagnostic(variable.Identifier.CreateDiagnostic(Rule));
}
}
}, SyntaxKind.FieldDeclaration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
RS0040 | RoslynDiagnosticsReliability | Warning | DefaultableTypeShouldHaveDefaultableFieldsAnalyzer
RS0042 | RoslynDiagnosticsDesign | Disabled | AvoidOptSuffixForNullableEnableCode

### Changed Rules
Rule ID | New Category | New Severity | Old Category | Old Severity | Notes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Roslyn.Diagnostics.Analyzers
{
public abstract class AvoidOptSuffixForNullableEnableCode : DiagnosticAnalyzer
{
private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(RoslynDiagnosticsAnalyzersResources.AvoidOptSuffixForNullableEnableCodeRuleIdTitle), RoslynDiagnosticsAnalyzersResources.ResourceManager, typeof(RoslynDiagnosticsAnalyzersResources));

private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(RoslynDiagnosticsAnalyzersResources.AvoidOptSuffixForNullableEnableCodeRuleIdMessage), RoslynDiagnosticsAnalyzersResources.ResourceManager, typeof(RoslynDiagnosticsAnalyzersResources));
private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString(nameof(RoslynDiagnosticsAnalyzersResources.AvoidOptSuffixForNullableEnableCodeRuleIdDescription), RoslynDiagnosticsAnalyzersResources.ResourceManager, typeof(RoslynDiagnosticsAnalyzersResources));

internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(
RoslynDiagnosticIds.AvoidOptSuffixForNullableEnableCodeRuleId,
s_localizableTitle,
s_localizableMessage,
DiagnosticCategory.RoslynDiagnosticsDesign,
DiagnosticSeverity.Warning,
isEnabledByDefault: false,
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
description: s_localizableDescription,
helpLinkUri: null,
customTags: WellKnownDiagnosticTags.Telemetry);

public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ internal static class RoslynDiagnosticIds
public const string RelaxTestNamingSuppressionRuleId = "RS0039";
public const string DefaultableTypeShouldHaveDefaultableFieldsRuleId = "RS0040";
public const string ObliviousPublicApiRuleId = "RS0041";
public const string AvoidOptSuffixForNullableEnableCodeRuleId = "RS0042";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,13 @@
<data name="DefaultableTypeShouldHaveDefaultableFieldsTitle" xml:space="preserve">
<value>Defaultable types should have defaultable fields</value>
</data>
<data name="AvoidOptSuffixForNullableEnableCodeRuleIdDescription" xml:space="preserve">
<value>Avoid the 'Opt' suffix in a nullable-enabled code.</value>
</data>
<data name="AvoidOptSuffixForNullableEnableCodeRuleIdMessage" xml:space="preserve">
<value>Avoid the 'Opt' suffix in a nullable-enabled code</value>
</data>
<data name="AvoidOptSuffixForNullableEnableCodeRuleIdTitle" xml:space="preserve">
<value>Avoid the 'Opt' suffix</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">Typy, které se dají nastavit jako výchozí, by měly mít pole, která se tak dají nastavit taky.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">Auf den Standard festlegbare Typen müssen auf den Standard festlegbare Felder aufweisen.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">Los tipos defaultable deben tener campos defaultable</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">Les types par défaut doivent avoir des champs par défaut</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">I tipi defaultable devono avere campi defaultable</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">既定に設定可能な型には、既定に設定可能なフィールドが必要です</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">기본값 설정 가능 형식에는 기본값 설정 가능 필드가 있어야 합니다.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">Typy dopuszczające wartość domyślną powinny mieć pola z wartościami domyślnymi</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">Os tipos usados como padrão devem ter campos usados como padrão</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ru" original="../RoslynDiagnosticsAnalyzersResources.resx">
<body>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdDescription">
<source>Avoid the 'Opt' suffix in a nullable-enabled code.</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code.</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdMessage">
<source>Avoid the 'Opt' suffix in a nullable-enabled code</source>
<target state="new">Avoid the 'Opt' suffix in a nullable-enabled code</target>
<note />
</trans-unit>
<trans-unit id="AvoidOptSuffixForNullableEnableCodeRuleIdTitle">
<source>Avoid the 'Opt' suffix</source>
<target state="new">Avoid the 'Opt' suffix</target>
<note />
</trans-unit>
<trans-unit id="DefaultableTypeShouldHaveDefaultableFieldsDescription">
<source>Defaultable types should have defaultable fields</source>
<target state="translated">Типы, допускающие значения по умолчанию, должны иметь соответствующие поля.</target>
Expand Down
Loading