Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jun 20, 2021
1 parent 8b06418 commit 8530db9
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/Analyzers/CSharp/Analyzers/CSharpAnalyzersResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@
<data name="Use_is_null_check" xml:space="preserve">
<value>Use 'is null' check</value>
</data>
<data name="Use_is_not_null_check" xml:space="preserve">
<value>Use 'is not null' check</value>
</data>
<data name="Use_simple_using_statement" xml:space="preserve">
<value>Use simple 'using' statement</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
Expand All @@ -16,12 +17,22 @@ namespace Microsoft.CodeAnalysis.CSharp.UseIsNullCheck
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor s_useIsNullDescriptor = CreateDescriptorWithId(
IDEDiagnosticIds.UseNullCheckOverTypeCheckDiagnosticId,
EnforceOnBuildValues.UseNullCheckOverTypeCheck,
new LocalizableResourceString(nameof(CSharpAnalyzersResources.Use_is_null_check), AnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)),
new LocalizableResourceString(nameof(CSharpAnalyzersResources.Null_check_can_be_clarified), AnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)));

private static readonly DiagnosticDescriptor s_useIsNotNullDescriptor = CreateDescriptorWithId(
IDEDiagnosticIds.UseNullCheckOverTypeCheckDiagnosticId,
EnforceOnBuildValues.UseNullCheckOverTypeCheck,
new LocalizableResourceString(nameof(CSharpAnalyzersResources.Use_is_not_null_check), AnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)),
new LocalizableResourceString(nameof(CSharpAnalyzersResources.Null_check_can_be_clarified), AnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)));

private static readonly ImmutableArray<DiagnosticDescriptor> s_descriptors = ImmutableArray.Create(s_useIsNullDescriptor, s_useIsNotNullDescriptor);

public CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer()
: base(IDEDiagnosticIds.UseNullCheckOverTypeCheckDiagnosticId,
EnforceOnBuildValues.UseNullCheckOverTypeCheck,
CSharpCodeStyleOptions.PreferNullCheckOverTypeCheck,
CSharpAnalyzersResources.Use_is_null_check,
new LocalizableResourceString(nameof(CSharpAnalyzersResources.Null_check_can_be_clarified), AnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)))
: base(s_descriptors)
{
}

Expand Down Expand Up @@ -51,23 +62,24 @@ private void AnalyzeOperation(OperationAnalysisContext context)
return;
}

if (ShouldReportDiagnostic(context.Operation) &&
if (ShouldReportDiagnostic(context.Operation, out var descriptor) &&
context.Operation.Syntax is BinaryExpressionSyntax or UnaryPatternSyntax)
{
var severity = option.Notification.Severity;
context.ReportDiagnostic(
DiagnosticHelper.Create(
Descriptor, context.Operation.Syntax.GetLocation(), severity, additionalLocations: null, properties: null));
descriptor, context.Operation.Syntax.GetLocation(), severity, additionalLocations: null, properties: null));
}
}

private static bool ShouldReportDiagnostic(IOperation operation)
private static bool ShouldReportDiagnostic(IOperation operation, out DiagnosticDescriptor descriptor)
{
if (operation is IIsTypeOperation isTypeOperation)
{
// Matches 'x is MyType'
// isTypeOperation.TypeOperand is 'MyType'
// isTypeOperation.ValueOperand.Type is the type of 'x'.
descriptor = s_useIsNotNullDescriptor;
return isTypeOperation.ValueOperand.Type is not null &&
isTypeOperation.ValueOperand.Type.InheritsFromOrEquals(isTypeOperation.TypeOperand);
}
Expand All @@ -76,6 +88,7 @@ private static bool ShouldReportDiagnostic(IOperation operation)
// Matches 'x is not MyType'
// InputType is the type of 'x'
// MatchedType is 'MyType'
descriptor = s_useIsNullDescriptor;
return negatedPattern.Pattern is ITypePatternOperation typePatternOperation &&
typePatternOperation.InputType.InheritsFromOrEquals(typePatternOperation.MatchedType);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8530db9

Please sign in to comment.