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

Merge main to main-vs-deps #54446

Merged
29 commits merged into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
245412f
Implement 'Prefer is null over is object'
Youssef1313 Jun 8, 2021
b56d3b5
Delete incorrectly checked-in file
Youssef1313 Jun 8, 2021
acba69d
Feedback
Youssef1313 Jun 8, 2021
bf97836
Make analyzer more general and add codefix
Youssef1313 Jun 8, 2021
2abf4dd
Renames
Youssef1313 Jun 8, 2021
2371e98
Rename
Youssef1313 Jun 8, 2021
fdbd4cd
Fix
Youssef1313 Jun 8, 2021
0a08f1e
Address feedback
Youssef1313 Jun 20, 2021
8b06418
Fix tests
Youssef1313 Jun 20, 2021
8530db9
Address feedback
Youssef1313 Jun 20, 2021
387cb88
Make static
Youssef1313 Jun 21, 2021
3cf3780
Update IDEDiagnosticIDConfigurationTests.cs
Youssef1313 Jun 21, 2021
6ecffc1
Revert "Address feedback"
Youssef1313 Jun 22, 2021
41d74e6
Fix resources
Youssef1313 Jun 22, 2021
b391668
Feedback
Youssef1313 Jun 22, 2021
3e9a888
Merge branch 'use-is-null' of https://GitHub.com/Youssef1313/Roslyn i…
Youssef1313 Jun 22, 2021
ea43edb
Address feedback
Youssef1313 Jun 22, 2021
33c1440
Add single quotes around null
Youssef1313 Jun 22, 2021
22ac244
Fix tests and rename fix provider name
Youssef1313 Jun 23, 2021
0254fbd
Fix add parameter for records
Youssef1313 Jun 27, 2021
8b51a85
Simplify delay
CyrusNajmabadi Jun 27, 2021
aa4b86f
Simplify delay
CyrusNajmabadi Jun 27, 2021
16b2d20
Move to a simpler event source system for nav bars.
CyrusNajmabadi Jun 27, 2021
b97477b
remove semantic version check
CyrusNajmabadi Jun 27, 2021
e3ea8d9
Docs
CyrusNajmabadi Jun 27, 2021
ba968b8
Fix test
CyrusNajmabadi Jun 28, 2021
48dd54f
Merge pull request #53947 from Youssef1313/use-is-null
CyrusNajmabadi Jun 28, 2021
3383013
Merge pull request #54427 from Youssef1313/add-param-records
CyrusNajmabadi Jun 28, 2021
d547c1e
Merge pull request #54432 from CyrusNajmabadi/taggerEvents
CyrusNajmabadi Jun 28, 2021
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
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseIndexOrRangeOperator\Helpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseIndexOrRangeOperator\MemberInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseInferredMemberName\CSharpUseInferredMemberNameDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseIsNullCheck\CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseIsNullCheck\CSharpUseIsNullCheckForCastAndEqualityOperatorDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseIsNullCheck\CSharpUseIsNullCheckForReferenceEqualsDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseLocalFunction\CSharpUseLocalFunctionDiagnosticAnalyzer.cs" />
Expand Down
6 changes: 6 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="Prefer_null_check_over_type_check" xml:space="preserve">
<value>Prefer 'null' check over type check</value>
</data>
<data name="Use_simple_using_statement" xml:space="preserve">
<value>Use simple 'using' statement</value>
</data>
Expand Down Expand Up @@ -314,4 +317,7 @@
<data name="Blank_line_not_allowed_after_constructor_initializer_colon" xml:space="preserve">
<value>Blank line not allowed after constructor initializer colon</value>
</data>
<data name="Null_check_can_be_clarified" xml:space="preserve">
<value>Null check can be clarified</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.CSharp.UseIsNullCheck
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
public CSharpUseNullCheckOverTypeCheckDiagnosticAnalyzer()
: base(IDEDiagnosticIds.UseNullCheckOverTypeCheckDiagnosticId,
EnforceOnBuildValues.UseNullCheckOverTypeCheck,
CSharpCodeStyleOptions.PreferNullCheckOverTypeCheck,
CSharpAnalyzersResources.Prefer_null_check_over_type_check,
new LocalizableResourceString(nameof(CSharpAnalyzersResources.Null_check_can_be_clarified), AnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources)))
{
}

public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterCompilationStartAction(context =>
{
if (((CSharpCompilation)context.Compilation).LanguageVersion < LanguageVersion.CSharp9)
{
return;
}

context.RegisterOperationAction(c => AnalyzeIsTypeOperation(c), OperationKind.IsType);
context.RegisterOperationAction(c => AnalyzeNegatedPatternOperation(c), OperationKind.NegatedPattern);
});
}

private static bool ShouldAnalyze(OperationAnalysisContext context, out ReportDiagnostic severity)
{
var option = context.Options.GetOption(CSharpCodeStyleOptions.PreferNullCheckOverTypeCheck, context.Operation.Syntax.SyntaxTree, context.CancellationToken);
if (!option.Value)
{
severity = ReportDiagnostic.Default;
return false;
}

severity = option.Notification.Severity;
return true;
}

private void AnalyzeNegatedPatternOperation(OperationAnalysisContext context)
{
if (!ShouldAnalyze(context, out var severity) ||
context.Operation.Syntax is not UnaryPatternSyntax)
{
return;
}

var negatedPattern = (INegatedPatternOperation)context.Operation;
// Matches 'x is not MyType'
// InputType is the type of 'x'
// MatchedType is 'MyType'
// We check InheritsFromOrEquals so that we report a diagnostic on the following:
// 1. x is not object (which is also equivalent to 'is null' check)
// 2. derivedObj is parentObj (which is the same as the previous point).
// 3. str is string (where str is a string, this is also equivalent to 'is null' check).
// This doesn't match `x is not MyType y` because in such case, negatedPattern.Pattern will
// be `DeclarationPattern`, not `TypePattern`.
if (negatedPattern.Pattern is ITypePatternOperation typePatternOperation &&
typePatternOperation.InputType.InheritsFromOrEquals(typePatternOperation.MatchedType))
{
context.ReportDiagnostic(
DiagnosticHelper.Create(
Descriptor, context.Operation.Syntax.GetLocation(), severity, additionalLocations: null, properties: null));
}
}

private void AnalyzeIsTypeOperation(OperationAnalysisContext context)
{
if (!ShouldAnalyze(context, out var severity) ||
context.Operation.Syntax is not BinaryExpressionSyntax)
{
return;
}

var isTypeOperation = (IIsTypeOperation)context.Operation;
// Matches 'x is MyType'
// isTypeOperation.TypeOperand is 'MyType'
// isTypeOperation.ValueOperand.Type is the type of 'x'.
// We check InheritsFromOrEquals for the same reason as stated in AnalyzeNegatedPatternOperation.
// This doesn't match `x is MyType y` because in such case, we have an IsPattern instead of IsType operation.
if (isTypeOperation.ValueOperand.Type is not null &&
isTypeOperation.ValueOperand.Type.InheritsFromOrEquals(isTypeOperation.TypeOperand))
{
context.ReportDiagnostic(
DiagnosticHelper.Create(
Descriptor, context.Operation.Syntax.GetLocation(), severity, additionalLocations: null, properties: null));
}
}
}
}
10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.cs.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.de.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.es.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.fr.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.it.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.ja.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.ko.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.pl.xlf

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.

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.ru.xlf

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

10 changes: 10 additions & 0 deletions src/Analyzers/CSharp/Analyzers/xlf/CSharpAnalyzersResources.tr.xlf

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.

1 change: 1 addition & 0 deletions src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseInferredMemberName\CSharpUseInferredMemberNameCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseIsNullCheck\CSharpUseIsNullCheckForCastAndEqualityOperatorCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseIsNullCheck\CSharpUseIsNullCheckForReferenceEqualsCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseIsNullCheck\CSharpUseNullCheckOverTypeCheckCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseNullPropagation\CSharpUseNullPropagationCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseObjectInitializer\CSharpUseObjectInitializerCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseObjectInitializer\UseInitializerHelpers.cs" />
Expand Down
Loading