Skip to content
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
Expand Up @@ -126,8 +126,7 @@ void FadeOutCode(SyntaxNodeAnalysisContext context, AnalysisResult analysisResul
if (createSymbol is not IMethodSymbol { IsStatic: true } createMethod)
return null;

var factoryType = semanticModel.GetSymbolInfo(memberAccessExpression.Expression, cancellationToken).Symbol as INamedTypeSymbol;
if (factoryType is null)
if (semanticModel.GetSymbolInfo(memberAccessExpression.Expression, cancellationToken).Symbol is not INamedTypeSymbol factoryType)
return null;

// has to be the form:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ internal sealed class CSharpChangeToIEnumerableCodeFixProvider() : AbstractItera
newDocument = document.WithSyntaxRoot(root.ReplaceNode(node, newOperator));
}

var oldAccessor = node.Parent?.Parent as PropertyDeclarationSyntax;
if (oldAccessor != null)
if (node.Parent?.Parent is PropertyDeclarationSyntax oldAccessor)
{
newDocument = document.WithSyntaxRoot(root.ReplaceNode(oldAccessor, oldAccessor.WithType(newReturnType)));
}

var oldIndexer = node.Parent?.Parent as IndexerDeclarationSyntax;
if (oldIndexer != null)
if (node.Parent?.Parent is IndexerDeclarationSyntax oldIndexer)
{
newDocument = document.WithSyntaxRoot(root.ReplaceNode(oldIndexer, oldIndexer.WithType(newReturnType)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ protected override Task FixAllAsync(
{
foreach (var diagnostic in diagnostics)
{
var anonymousFunction = diagnostic.AdditionalLocations[0].FindNode(getInnermostNodeForTie: true, cancellationToken) as AnonymousFunctionExpressionSyntax;
if (anonymousFunction is null)
if (diagnostic.AdditionalLocations[0].FindNode(getInnermostNodeForTie: true, cancellationToken) is not AnonymousFunctionExpressionSyntax anonymousFunction)
continue;

editor.ReplaceNode(anonymousFunction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ private bool TryAnalyzeMultiAddInvocation(
// values)` If the former, we only allow a single argument. If the latter, we can allow multiple
// expressions. The former will be converted to a spread element. The latter will be added
// individually.
var method = this.SemanticModel.GetSymbolInfo(memberAccess, cancellationToken).GetAnySymbol() as IMethodSymbol;
if (method is null)
if (this.SemanticModel.GetSymbolInfo(memberAccess, cancellationToken).GetAnySymbol() is not IMethodSymbol method)
return false;

if (method.Parameters.Length != 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ private async Task FixOneDiagnosticAsync(
var hasMissingDefaultCase = bool.Parse(diagnostic.Properties[PopulateSwitchStatementHelpers.MissingDefaultCase]!);

var switchLocation = diagnostic.AdditionalLocations[0];
var switchNode = switchLocation.FindNode(getInnermostNodeForTie: true, cancellationToken) as TSwitchSyntax;
if (switchNode == null)
if (switchLocation.FindNode(getInnermostNodeForTie: true, cancellationToken) is not TSwitchSyntax switchNode)
return;

var model = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,11 @@
// If we didn't find an object name above, then the object name is the name of this class.
// Note: For generic, it's ok(it's even a good idea) to exclude type variables,
// because the name is only used as a prefix for the method name.

Check failure on line 250 in src/EditorFeatures/CSharp/EventHookup/EventHookupSessionManager_EventHookupSession.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/EditorFeatures/CSharp/EventHookup/EventHookupSessionManager_EventHookupSession.cs#L250

src/EditorFeatures/CSharp/EventHookup/EventHookupSessionManager_EventHookupSession.cs(250,1): error IDE2000: (NETCORE_ENGINEERING_TELEMETRY=Build) Avoid multiple blank lines (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide2000)

Check failure on line 250 in src/EditorFeatures/CSharp/EventHookup/EventHookupSessionManager_EventHookupSession.cs

View check run for this annotation

Azure Pipelines / roslyn-CI

src/EditorFeatures/CSharp/EventHookup/EventHookupSessionManager_EventHookupSession.cs#L250

src/EditorFeatures/CSharp/EventHookup/EventHookupSessionManager_EventHookupSession.cs(250,1): error IDE2000: (NETCORE_ENGINEERING_TELEMETRY=Build) Avoid multiple blank lines (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide2000)
var typeDeclaration = syntaxFactsService.GetContainingTypeDeclaration(
semanticModel.SyntaxTree.GetRoot(),
plusEqualsToken.SpanStart) as BaseTypeDeclarationSyntax;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

return typeDeclaration != null
return syntaxFactsService.GetContainingTypeDeclaration(
semanticModel.SyntaxTree.GetRoot(),
plusEqualsToken.SpanStart) is BaseTypeDeclarationSyntax typeDeclaration
? typeDeclaration.Identifier.Text
: eventSymbol.ContainingType.Name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ private bool IsTriggerToken(SyntaxToken token)
return null;

var baseList = baseTypeSyntax.Parent as BaseListSyntax;
var namedTypeSyntax = baseList?.Parent as BaseTypeDeclarationSyntax;
if (namedTypeSyntax is null)
if (baseList?.Parent is not BaseTypeDeclarationSyntax namedTypeSyntax)
return null;

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ private static (ISymbol? symbol, int overloadCount) GetSymbolAndOverloadCount(Co
// If we have SymbolKey data (i.e. this is an extension method item), use it to recover symbol
if (item.TryGetProperty(MethodKey, out var methodSymbolKey))
{
var methodSymbol = SymbolKey.ResolveString(methodSymbolKey, compilation).GetAnySymbol() as IMethodSymbol;

if (methodSymbol != null)
if (SymbolKey.ResolveString(methodSymbolKey, compilation).GetAnySymbol() is IMethodSymbol methodSymbol)
{
var overloadCount = item.TryGetProperty(OverloadCountKey, out var overloadCountString) && int.TryParse(overloadCountString, out var count) ? count : 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ private static ImmutableArray<char> GetCommitCharacters()

protected override async Task ProvideCompletionsAsync(CompletionContext context, string pathThroughLastSlash)
{
var resolver = context.Document.Project.CompilationOptions.MetadataReferenceResolver as RuntimeMetadataReferenceResolver;
if (resolver != null && pathThroughLastSlash.IndexOfAny(s_pathIndicators) < 0)
if (context.Document.Project.CompilationOptions.MetadataReferenceResolver is RuntimeMetadataReferenceResolver resolver && pathThroughLastSlash.IndexOfAny(s_pathIndicators) < 0)
{
foreach (var (name, path) in resolver.TrustedPlatformAssemblies)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
if (comparableType == null)
return;

var containingType = semanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken) as INamedTypeSymbol;
if (containingType == null)
if (semanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken) is not INamedTypeSymbol containingType)
return;

using var _1 = ArrayBuilder<INamedTypeSymbol>.GetInstance(out var missingComparableTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var calleeMethodSymbol = semanticModel.GetSymbolInfo(calleeInvocationNode, cancellationToken).GetAnySymbol() as IMethodSymbol;
if (calleeMethodSymbol == null)
if (semanticModel.GetSymbolInfo(calleeInvocationNode, cancellationToken).GetAnySymbol() is not IMethodSymbol calleeMethodSymbol)
{
return;
}
Expand Down Expand Up @@ -125,8 +124,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}

var calleeMethodDeclarationSyntaxReference = calleeMethodDeclarationSyntaxReferences[0];
var calleeMethodNode = await calleeMethodDeclarationSyntaxReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false) as TMethodDeclarationSyntax;
if (calleeMethodNode == null)
if (await calleeMethodDeclarationSyntaxReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false) is not TMethodDeclarationSyntax calleeMethodNode)
{
return;
}
Expand Down Expand Up @@ -222,8 +220,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}

var callerDeclarationNode = await callerReferences[0].GetSyntaxAsync(cancellationToken).ConfigureAwait(false);
var invocationOperation = semanticModel.GetOperation(calleeInvocationNode, cancellationToken) as IInvocationOperation;
if (invocationOperation == null)
if (semanticModel.GetOperation(calleeInvocationNode, cancellationToken) is not IInvocationOperation invocationOperation)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public async Task ComputeRefactoringsAsync(

// We allow empty nodes here to find VB implicit arguments.
var potentialArguments = await document.GetRelevantNodesAsync<TBaseArgumentSyntax>(textSpan, allowEmptyNodes: true, cancellationToken).ConfigureAwait(false);
var argument = potentialArguments.FirstOrDefault(n => n.Parent is TArgumentListSyntax) as TSimpleArgumentSyntax;
if (argument == null)
if (potentialArguments.FirstOrDefault(n => n.Parent is TArgumentListSyntax) is not TSimpleArgumentSyntax argument)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ public async Task<bool> TrySymbolNavigationNotifyAsync(ISymbol symbol, Project p
if (!TryGetVsHierarchyAndItemId(documentToUse, out var hierarchy, out var itemID))
return null;

var navigationNotify = hierarchy as IVsSymbolicNavigationNotify;
if (navigationNotify == null)
if (hierarchy is not IVsSymbolicNavigationNotify navigationNotify)
return null;

return (hierarchy, itemID, navigationNotify);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ private void UpdateSeverityMenuItemsChecked()
_setSeverityHiddenMenuItem.Checked = false;
_setSeverityNoneMenuItem.Checked = false;

var workspace = TryGetWorkspace() as VisualStudioWorkspace;
if (workspace == null)
if (TryGetWorkspace() is not VisualStudioWorkspace workspace)
{
return;
}
Expand Down Expand Up @@ -369,9 +368,8 @@ internal void OpenRuleSetHandler(object sender, EventArgs args)
if (_tracker.SelectedFolder != null &&
_serviceProvider != null)
{
var workspace = _tracker.SelectedFolder.Workspace as VisualStudioWorkspace;
var projectId = _tracker.SelectedFolder.ProjectId;
if (workspace != null)
if (_tracker.SelectedFolder.Workspace is VisualStudioWorkspace workspace)
{
var ruleSetFile = workspace.TryGetRuleSetPathForProject(projectId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ public bool ShowContextMenu(IEnumerable<object> items, Point location)

_updateMenu();

var shell = Shell.Package.GetGlobalService(typeof(SVsUIShell)) as IVsUIShell;
var guidContextMenu = Guids.RoslynGroupId;
var locationPoints = new[] { new POINTS() { x = (short)location.X, y = (short)location.Y } };
return shell != null && ErrorHandler.Succeeded(shell.ShowContextMenu(
return Shell.Package.GetGlobalService(typeof(SVsUIShell)) is IVsUIShell shell && ErrorHandler.Succeeded(shell.ShowContextMenu(
0,
ref guidContextMenu,
_menuId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ private static void BuildStringFromInlineCollection(InlineCollection inlines, St
if (currentInline is LineBreak)
return Environment.NewLine;

var run = currentInline as Run;
if (run == null)
if (currentInline is not Run run)
return null;

return run.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public InternalOptionsControl(IEnumerable<IOption2> options, OptionStore optionS
{
foreach (var item in optionsPanel.Children.OfType<CheckBox>())
{
var title = item.Content as string;
if (title == null)
if (item.Content is not string title)
{
continue;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Workspaces/Core/Portable/Log/AggregateLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public static ILogger AddOrReplace(ILogger newLogger, ILogger oldLogger, Func<IL
return newLogger;
}

var aggregateLogger = oldLogger as AggregateLogger;
if (aggregateLogger == null)
if (oldLogger is not AggregateLogger aggregateLogger)
{
// replace old logger with new logger
if (predicate(oldLogger))
Expand Down Expand Up @@ -86,8 +85,7 @@ public static ILogger AddOrReplace(ILogger newLogger, ILogger oldLogger, Func<IL

public static ILogger Remove(ILogger logger, Func<ILogger, bool> predicate)
{
var aggregateLogger = logger as AggregateLogger;
if (aggregateLogger == null)
if (logger is not AggregateLogger aggregateLogger)
{
// remove the logger
if (predicate(logger))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ private static XNode[] RewriteInheritdocElements(ISymbol symbol, HashSet<ISymbol
}
}

var container = node as XContainer;
if (container == null)
if (node is not XContainer container)
{
return [Copy(node, copyAttributeAnnotations: false)];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ protected override Stream GetSourceStream(CancellationToken cancellationToken)

public override bool Equals(object obj)
{
var other = obj as ContentBasedXmlDocumentationProvider;
return other != null && EqualsHelper(other);
return obj is ContentBasedXmlDocumentationProvider other && EqualsHelper(other);
}

private bool EqualsHelper(ContentBasedXmlDocumentationProvider other)
Expand Down Expand Up @@ -162,8 +161,7 @@ protected override Stream GetSourceStream(CancellationToken cancellationToken)

public override bool Equals(object obj)
{
var other = obj as FileBasedXmlDocumentationProvider;
return other != null && _filePath == other._filePath;
return obj is FileBasedXmlDocumentationProvider other && _filePath == other._filePath;
}

public override int GetHashCode()
Expand Down
3 changes: 1 addition & 2 deletions src/Workspaces/CoreTest/SolutionTests/SolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2866,8 +2866,7 @@ private static async Task ValidateSolutionAndCompilationsAsync(Solution solution
{
compilation.References.Single(r =>
{
var cr = r as CompilationReference;
return cr != null && cr.Compilation == compilationReference.Compilation;
return r is CompilationReference cr && cr.Compilation == compilationReference.Compilation;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ protected override IRemoteEncapsulateFieldService CreateService(in ServiceConstr

foreach (var key in fieldSymbolKeys)
{
var resolved = SymbolKey.ResolveString(key, compilation, cancellationToken: cancellationToken).GetAnySymbol() as IFieldSymbol;
if (resolved == null)
if (SymbolKey.ResolveString(key, compilation, cancellationToken: cancellationToken).GetAnySymbol() is not IFieldSymbol resolved)
return [];

fields.Add(resolved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ private static bool IsPrivateSymbolAccessible(
{
Debug.Assert(within is INamedTypeSymbol or IAssemblySymbol);

var withinType = within as INamedTypeSymbol;
if (withinType == null)
if (within is not INamedTypeSymbol withinType)
{
// If we're not within a type, we can't access a private symbol
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,9 @@ private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeS
// SPEC: argument is more specific and no type argument is less specific than the
// SPEC: corresponding type argument in the other.

var n1 = t1 as INamedTypeSymbol;
var n2 = t2 as INamedTypeSymbol;

if (n1 == null)
if (t1 is not INamedTypeSymbol n1)
{
return null;
}
Expand Down
Loading