提交 f5e63524 编写于 作者: C CyrusNajmabadi

Feature should not be offered unless user is targetting C# 7 and above.

上级 8f33c29f
......@@ -354,6 +354,7 @@
<Compile Include="Organizing\Organizers\OperatorDeclarationOrganizer.cs" />
<Compile Include="Organizing\Organizers\PropertyDeclarationOrganizer.cs" />
<Compile Include="Organizing\Organizers\StructDeclarationOrganizer.cs" />
<Compile Include="SimplifyNullCheck\CSharpSimplifyNullCheckDiagnosticAnalyzer.cs" />
<Compile Include="Structure\CSharpBlockStructureProvider.cs" />
<Compile Include="Structure\CSharpStructureHelpers.cs" />
<Compile Include="Structure\CSharpBlockStructureService.cs" />
......
// 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 Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.SimplifyNullCheck;
namespace Microsoft.CodeAnalysis.CSharp.SimplifyNullCheck
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class CSharpSimplifyNullCheckDiagnosticAnalyzer : AbstractSimplifyNullCheckDiagnosticAnalyzer
{
protected override bool IsSupported(ParseOptions options)
{
var csOptions = (CSharpParseOptions)options;
return csOptions.LanguageVersion >= LanguageVersion.CSharp7;
}
}
}
\ No newline at end of file
......@@ -92,7 +92,7 @@
</Compile>
<Compile Include="AddMissingReference\AbstractAddMissingReferenceCodeFixProvider.cs" />
<Compile Include="AddMissingReference\CodeAction.cs" />
<Compile Include="SimplifyNullCheck\SimplifyNullCheckAnalyzer.cs" />
<Compile Include="SimplifyNullCheck\AbstractSimplifyNullCheckDiagnosticAnalyzer.cs" />
<Compile Include="SimplifyNullCheck\SimplifyNullCheckCodeFixProvider.cs" />
<Compile Include="Structure\BlockTypes.cs" />
<Compile Include="CodeLens\CodeLensReferencesServiceFactory.cs" />
......
// 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Semantics;
......@@ -32,8 +28,7 @@ namespace Microsoft.CodeAnalysis.SimplifyNullCheck
/// Note: this analyzer can be udpated to run on VB once VB supports 'throw'
/// expressions as well.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class SimplifyNullCheckAnalyzer : DiagnosticAnalyzer, IBuiltInAnalyzer
internal abstract class AbstractSimplifyNullCheckDiagnosticAnalyzer : DiagnosticAnalyzer, IBuiltInAnalyzer
{
private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(FeaturesResources.Simplify_null_check), FeaturesResources.ResourceManager, typeof(FeaturesResources));
private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(FeaturesResources.Simplify_null_check), WorkspacesResources.ResourceManager, typeof(WorkspacesResources));
......@@ -60,6 +55,8 @@ public DiagnosticAnalyzerCategory GetAnalyzerCategory()
private static MethodInfo s_getOperationInfo =
typeof(SemanticModel).GetTypeInfo().GetDeclaredMethod("GetOperationInternal");
protected abstract bool IsSupported(ParseOptions options);
public override void Initialize(AnalysisContext context)
{
s_registerOperationActionInfo.Invoke(context, new object[]
......@@ -69,14 +66,19 @@ public override void Initialize(AnalysisContext context)
});
}
private void AnalyzeOperation(OperationAnalysisContext operationContext)
private void AnalyzeOperation(OperationAnalysisContext context)
{
var cancellationToken = operationContext.CancellationToken;
if (!IsSupported(context.Operation.Syntax.SyntaxTree.Options))
{
return;
}
var cancellationToken = context.CancellationToken;
var throwOperation = (IThrowStatement)operationContext.Operation;
var throwOperation = (IThrowStatement)context.Operation;
var throwStatement = throwOperation.Syntax;
var compilation = operationContext.Compilation;
var compilation = context.Compilation;
var semanticModel = compilation.GetSemanticModel(throwStatement.SyntaxTree);
var ifOperation = GetContainingIfOperation(
......@@ -127,9 +129,9 @@ private void AnalyzeOperation(OperationAnalysisContext operationContext)
throwOperation.ThrownObject.Syntax.GetLocation(),
assignmentExpression.Value.Syntax.GetLocation());
operationContext.ReportDiagnostic(
context.ReportDiagnostic(
Diagnostic.Create(s_descriptor, ifOperation.Syntax.GetLocation(), additionalLocations: allLocations));
operationContext.ReportDiagnostic(
context.ReportDiagnostic(
Diagnostic.Create(s_descriptor, expressionStatement.Syntax.GetLocation(), additionalLocations: allLocations));
}
......
// 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
......
......@@ -3478,6 +3478,11 @@ public override SyntaxNode ThrowStatement(SyntaxNode expressionOpt = null)
return SyntaxFactory.ThrowStatement((ExpressionSyntax)expressionOpt);
}
public override SyntaxNode ThrowExpression(SyntaxNode expression)
{
return SyntaxFactory.ThrowExpression((ExpressionSyntax)expression);
}
public override SyntaxNode IfStatement(SyntaxNode condition, IEnumerable<SyntaxNode> trueStatements, IEnumerable<SyntaxNode> falseStatements = null)
{
if (falseStatements == null)
......
......@@ -1302,6 +1302,11 @@ protected static SyntaxList<TNode> RemoveRange<TNode>(SyntaxList<TNode> list, in
/// <param name="expression">An optional expression that can be thrown.</param>
public abstract SyntaxNode ThrowStatement(SyntaxNode expression = null);
/// <summary>
/// Creates an expression that can be used to throw an exception.
/// </summary>
public abstract SyntaxNode ThrowExpression(SyntaxNode expression);
/// <summary>
/// Creates a statement that declares a single local variable.
/// </summary>
......
......@@ -27,9 +27,9 @@ Microsoft.CodeAnalysis.Options.DocumentOptionSet.GetOption<T>(Microsoft.CodeAnal
Microsoft.CodeAnalysis.Options.IOption.StorageLocations.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Options.OptionStorageLocation>
Microsoft.CodeAnalysis.Options.Option<T>.Option(string feature, string name, T defaultValue, params Microsoft.CodeAnalysis.Options.OptionStorageLocation[] storageLocations) -> void
Microsoft.CodeAnalysis.Options.Option<T>.StorageLocations.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Options.OptionStorageLocation>
Microsoft.CodeAnalysis.Options.OptionSet.OptionSet() -> void
Microsoft.CodeAnalysis.Options.OptionStorageLocation
Microsoft.CodeAnalysis.Options.OptionStorageLocation.OptionStorageLocation() -> void
Microsoft.CodeAnalysis.Options.OptionSet.OptionSet() -> void
Microsoft.CodeAnalysis.Options.PerLanguageOption<T>.PerLanguageOption(string feature, string name, T defaultValue, params Microsoft.CodeAnalysis.Options.OptionStorageLocation[] storageLocations) -> void
Microsoft.CodeAnalysis.Options.PerLanguageOption<T>.StorageLocations.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Options.OptionStorageLocation>
Microsoft.CodeAnalysis.Solution.Options.get -> Microsoft.CodeAnalysis.Options.OptionSet
......@@ -37,6 +37,7 @@ Microsoft.CodeAnalysis.Workspace.DocumentActiveContextChanged -> System.EventHan
Microsoft.CodeAnalysis.Workspace.RaiseDocumentActiveContextChangedEventAsync(Microsoft.CodeAnalysis.Text.SourceTextContainer sourceTextContainer, Microsoft.CodeAnalysis.DocumentId oldActiveContextDocumentId, Microsoft.CodeAnalysis.DocumentId newActiveContextDocumentId) -> System.Threading.Tasks.Task
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.GetSwitchSections(Microsoft.CodeAnalysis.SyntaxNode switchStatement) -> System.Collections.Generic.IReadOnlyList<Microsoft.CodeAnalysis.SyntaxNode>
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.InsertSwitchSections(Microsoft.CodeAnalysis.SyntaxNode switchStatement, int index, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxNode> switchSections) -> Microsoft.CodeAnalysis.SyntaxNode
abstract Microsoft.CodeAnalysis.Editing.SyntaxGenerator.ThrowExpression(Microsoft.CodeAnalysis.SyntaxNode expression) -> Microsoft.CodeAnalysis.SyntaxNode
override Microsoft.CodeAnalysis.CodeStyle.CodeStyleOption<T>.Equals(object obj) -> bool
override Microsoft.CodeAnalysis.CodeStyle.CodeStyleOption<T>.GetHashCode() -> int
override Microsoft.CodeAnalysis.CodeStyle.NotificationOption.ToString() -> string
......
......@@ -276,6 +276,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return SyntaxFactory.ThrowStatement(DirectCast(expressionOpt, ExpressionSyntax))
End Function
Public Overrides Function ThrowExpression(expression As SyntaxNode) As SyntaxNode
Throw New NotSupportedException("ThrowExpressions are not supported in Visual Basic")
End Function
Public Overrides Function TypeExpression(typeSymbol As ITypeSymbol) As SyntaxNode
Return typeSymbol.GenerateTypeSyntax()
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册