Skip to content

Commit

Permalink
Merge pull request #42264 from mavasani/PortUseThrowExpression
Browse files Browse the repository at this point in the history
Port UseThrowExpression analyzer/fixer to shared layer
  • Loading branch information
mavasani authored Mar 11, 2020
2 parents f833923 + 2e72ece commit 9bfcf96
Show file tree
Hide file tree
Showing 41 changed files with 167 additions and 152 deletions.
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/Analyzers/CSharpAnalyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
<Compile Include="$(MSBuildThisFileDirectory)UseImplicitOrExplicitType\CSharpTypeStyleDiagnosticAnalyzerBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseImplicitOrExplicitType\CSharpUseExplicitTypeDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseImplicitOrExplicitType\CSharpUseImplicitTypeDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\CSharpUseThrowExpressionDiagnosticAnalyzer.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
// 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.CSharp.CodeStyle;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.UseThrowExpression;

#if CODE_STYLE
using Microsoft.CodeAnalysis.CSharp.Internal.CodeStyle;
#else
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
#endif

namespace Microsoft.CodeAnalysis.CSharp.UseThrowExpression
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
Expand All @@ -23,7 +29,7 @@ protected override bool IsSupported(ParseOptions options)
return csOptions.LanguageVersion >= LanguageVersion.CSharp7;
}

protected override ISemanticFactsService GetSemanticFactsService()
=> CSharpSemanticFactsService.Instance;
protected override bool IsInExpressionTree(SemanticModel semanticModel, SyntaxNode node, INamedTypeSymbol expressionTypeOpt, CancellationToken cancellationToken)
=> node.IsInExpressionTree(semanticModel, expressionTypeOpt, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseImplicitOrExplicitType\UseExplicitTypeTests_FixAllTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseImplicitOrExplicitType\UseImplicitTypeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseImplicitOrExplicitType\UseImplicitTypeTests_FixAllTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\UseThrowExpressionTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\UseThrowExpressionTests_FixAllTests.cs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/Analyzers/Core/Analyzers/Analyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessaryCast\AbstractRemoveUnnecessaryCastDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnnecessaryImports\AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveUnusedMembers\AbstractRemoveUnusedMembersDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\AbstractUseThrowExpressionDiagnosticAnalyzer.cs" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions src/Analyzers/Core/Analyzers/AnalyzersResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
<data name="The_file_header_does_not_match_the_required_text" xml:space="preserve">
<value>The file header does not match the required text</value>
</data>
<data name="Use_throw_expression" xml:space="preserve">
<value>Use 'throw' expression</value>
</data>
<data name="Null_check_can_be_simplified" xml:space="preserve">
<value>Null check can be simplified</value>
</data>
<data name="Naming_rule_violation_0" xml:space="preserve">
<value>Naming rule violation: {0}</value>
<comment>{0} is the rule title, {1} is the way in which the rule was violated</comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;

#if CODE_STYLE
using Microsoft.CodeAnalysis.Internal.Options;
#else
using Microsoft.CodeAnalysis.Options;
#endif

namespace Microsoft.CodeAnalysis.UseThrowExpression
{
/// <summary>
Expand Down Expand Up @@ -41,8 +46,8 @@ protected AbstractUseThrowExpressionDiagnosticAnalyzer(Option<CodeStyleOption<bo
: base(IDEDiagnosticIds.UseThrowExpressionDiagnosticId,
preferThrowExpressionOption,
language,
new LocalizableResourceString(nameof(FeaturesResources.Use_throw_expression), FeaturesResources.ResourceManager, typeof(FeaturesResources)),
new LocalizableResourceString(nameof(FeaturesResources.Null_check_can_be_simplified), FeaturesResources.ResourceManager, typeof(FeaturesResources)))
new LocalizableResourceString(nameof(AnalyzersResources.Use_throw_expression), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
new LocalizableResourceString(nameof(AnalyzersResources.Null_check_can_be_simplified), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
{
_preferThrowExpressionOption = preferThrowExpressionOption;
}
Expand Down Expand Up @@ -99,8 +104,7 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
return;
}

var semanticFacts = GetSemanticFactsService();
if (semanticFacts.IsInExpressionTree(semanticModel, throwStatementSyntax, expressionTypeOpt, cancellationToken))
if (IsInExpressionTree(semanticModel, throwStatementSyntax, expressionTypeOpt, cancellationToken))
{
return;
}
Expand Down Expand Up @@ -173,7 +177,7 @@ private static bool ValueIsAccessed(SemanticModel semanticModel, IConditionalOpe
exprDataFlow.WrittenInside.Contains(localOrParameter);
}

protected abstract ISemanticFactsService GetSemanticFactsService();
protected abstract bool IsInExpressionTree(SemanticModel semanticModel, SyntaxNode node, INamedTypeSymbol expressionTypeOpt, CancellationToken cancellationToken);

private bool TryFindAssignmentExpression(
IBlockOperation containingBlock, IConditionalOperation ifOperation, ISymbol localOrParameter,
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">Naming rule violation: {0}</target>
<note>{0} is the rule title, {1} is the way in which the rule was violated</note>
</trans-unit>
<trans-unit id="Null_check_can_be_simplified">
<source>Null check can be simplified</source>
<target state="new">Null check can be simplified</target>
<note />
</trans-unit>
<trans-unit id="Private_member_0_can_be_removed_as_the_value_assigned_to_it_is_never_read">
<source>Private member '{0}' can be removed as the value assigned to it is never read.</source>
<target state="new">Private member '{0}' can be removed as the value assigned to it is never read.</target>
Expand Down Expand Up @@ -72,6 +77,11 @@
<target state="new">The file header is missing or not located at the top of the file</target>
<note />
</trans-unit>
<trans-unit id="Use_throw_expression">
<source>Use 'throw' expression</source>
<target state="new">Use 'throw' expression</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Loading

0 comments on commit 9bfcf96

Please sign in to comment.