Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -20,7 +20,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="MicroBuild.VisualStudio" Version="$(MicroBuildVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.3.2" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.8.2" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
<PackageReference Include="Microsoft.VisualStudio.OLE.Interop" Version="7.10.6070" />
<PackageReference Include="Microsoft.VisualStudio.Shell.14.0" Version="14.3.25407" IncludeAssets="runtime" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,25 @@ void F() {
await Verify.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task CastToVsSolutionViaIsWithPatternMatching()
{
var test = @"
using System;
using Microsoft.VisualStudio.Shell.Interop;

class Test {
void F() {
object obj1 = null;
if (obj1 is IVsSolution solution) {
}
}
}
";
var expected = Verify.Diagnostic(DescriptorSync).WithSpan(8, 18, 8, 32).WithArguments("IVsSolution", "Test.VerifyOnUIThread");
await Verify.VerifyAnalyzerAsync(test, expected);
}

/// <summary>
/// Verifies that the as cast operator does not produce a diagnostic when the type is to a managed type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.1</TargetFramework>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8</PackageTargetFallback>
<TargetFramework>netstandard1.3</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<CodeAnalysisRuleSet>Microsoft.VisualStudio.Threading.Analyzers.ruleset</CodeAnalysisRuleSet>
Expand All @@ -13,7 +12,7 @@
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageProjectUrl>https://github.com/Microsoft/vs-threading</PackageProjectUrl>
<PackageIconUrl>https://aka.ms/VsExtensibilityIcon</PackageIconUrl>
<PackageReleaseNotes>In the v15.8 release, analyzers are incomplete in the context of Visual Studio without the Microsoft.VisualStudio.Sdk.Analyzers 15.8 package also installed.</PackageReleaseNotes>
<PackageReleaseNotes>In the v16.0 release, analyzers require at least C# 7.3 (Visual Studio 2017 Update 7).</PackageReleaseNotes>

<DebugType>full</DebugType>

Expand Down Expand Up @@ -58,7 +57,7 @@
</PackageReference>
</ItemDefinitionGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="1.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.8.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.6.2" PrivateAssets="all" />
<PackageReference Include="MicroBuild.VisualStudio" Version="$(MicroBuildVersion)" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
Expand All @@ -81,4 +80,4 @@
<PackageLicenseUrl>https://github.com/Microsoft/vs-threading/$(GitCommitIdShort)/LICENSE</PackageLicenseUrl>
</PropertyGroup>
</Target>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Semantics;
using Microsoft.CodeAnalysis.Text;

/// <summary>
Expand Down Expand Up @@ -133,6 +132,7 @@ public override void Initialize(AnalysisContext context)
codeBlockStartContext.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(methodAnalyzer.AnalyzeCast), SyntaxKind.CastExpression);
codeBlockStartContext.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(methodAnalyzer.AnalyzeAs), SyntaxKind.AsExpression);
codeBlockStartContext.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(methodAnalyzer.AnalyzeAs), SyntaxKind.IsExpression);
codeBlockStartContext.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(methodAnalyzer.AnalyzeIsPattern), SyntaxKind.IsPatternExpression);
});

compilationStartContext.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(c => AddToCallerCalleeMap(c, callerToCalleeMap)), SyntaxKind.InvocationExpression);
Expand Down Expand Up @@ -369,6 +369,23 @@ internal void AnalyzeAs(SyntaxNodeAnalysisContext context)
}
}

internal void AnalyzeIsPattern(SyntaxNodeAnalysisContext context)
{
var patternSyntax = (IsPatternExpressionSyntax)context.Node;
if (patternSyntax.Pattern is DeclarationPatternSyntax declarationPatternSyntax && declarationPatternSyntax.Type != null)
{
var type = context.SemanticModel.GetSymbolInfo(declarationPatternSyntax.Type, context.CancellationToken).Symbol as ITypeSymbol;
if (type != null && IsObjectLikelyToBeCOMObject(type))
{
Location isAndTypeSide = Location.Create(
context.Node.SyntaxTree,
TextSpan.FromBounds(patternSyntax.IsKeyword.SpanStart,
declarationPatternSyntax.Type.Span.End));
this.AnalyzeMemberWithinContext(type, null, context, isAndTypeSide);
}
}
}

/// <summary>
/// Determines whether a given type is likely to be (or implemented by) a COM object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@
</ItemGroup>
<ItemGroup>
<!-- Don't consume the analyzers in this library itself,
but have a package dependency so users of this library will automatically get the analyzers. -->
<ProjectReference Include="..\Microsoft.VisualStudio.Threading.Analyzers\Microsoft.VisualStudio.Threading.Analyzers.csproj"
PrivateAssets="none" />
but have a package dependency so users of this library will automatically get the analyzers.
This requires an MSBuild workaround as documented here: https://github.com/Microsoft/msbuild/issues/2661#issuecomment-338808137
-->
<ProjectReference Include="..\Microsoft.VisualStudio.Threading.Analyzers\Microsoft.VisualStudio.Threading.Analyzers.csproj">
<PrivateAssets>none</PrivateAssets>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
<UndefineProperties>TargetFramework</UndefineProperties>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Multilingual App Toolkit\Microsoft.Multilingual.ResxResources.targets" Label="MultilingualAppToolkit" Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\Multilingual App Toolkit\v$(MultilingualAppToolkitVersion)\Microsoft.Multilingual.ResxResources.targets')" />
Expand Down