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 1 commit
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,26 @@
// 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.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
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
{
protected override bool IsNullableEnabledContext(CompilationOptions compilationOptions, IEnumerable<ParseOptions?> parseOptions)
{
var hasAnyCSharp8Part = parseOptions.OfType<CSharpParseOptions>().Any(option => option.LanguageVersion >= LanguageVersion.CSharp8);

return hasAnyCSharp8Part && IsNullableEnabledContext(compilationOptions);
}

private static bool IsNullableEnabledContext(CompilationOptions compilationOptions)
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
=> compilationOptions is CSharpCompilationOptions csharpCompilationOptions &&
csharpCompilationOptions.NullableContextOptions == NullableContextOptions.Enable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.Generic;
using System.Collections.Immutable;
using System.Linq;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
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 override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

public sealed override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

context.RegisterSymbolAction(context =>
{
if (context.Symbol.Name.EndsWith("Opt", System.StringComparison.Ordinal))
{
var parseOptions = context.Symbol.DeclaringSyntaxReferences
.Select(syntaxRef => syntaxRef.GetSyntax(context.CancellationToken)?.SyntaxTree?.Options);

if (IsNullableEnabledContext(context.Compilation.Options, parseOptions))
{
context.ReportDiagnostic(context.Symbol.CreateDiagnostic(Rule));
}
}
}, SymbolKind.Parameter, SymbolKind.Field);
}

protected abstract bool IsNullableEnabledContext(CompilationOptions compilationOptions, IEnumerable<ParseOptions?> parseOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ internal static class RoslynDiagnosticIds
public const string RestrictedInternalsVisibleToRuleId = "RS0035";
public const string AnnotatePublicApiRuleId = "RS0036";
public const string ShouldAnnotateApiFilesRuleId = "RS0037";
public const string AvoidOptSuffixForNullableEnableCodeRuleId = "RS0037";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,13 @@
<data name="FixNumberedComments" xml:space="preserve">
<value>Fix numbered comments</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Explicitně definujte importující konstruktor.</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Importierenden Konstruktor explizit definieren</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Definir explícitamente el constructor de importación</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Définir explicitement le constructeur d'importation</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Definire esplicitamente il costruttore di importazione</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Jawnie zdefiniuj konstruktor importujący</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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Definir explicitamente o construtor de importaçã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="ExportedPartsShouldHaveImportingConstructorCodeFix_ImplicitConstructor">
<source>Explicitly define the importing constructor</source>
<target state="translated">Явно определите конструктор импорта.</target>
Expand Down
Loading