Skip to content

Commit

Permalink
Add IsFileLocal lightup, use it in CA1708
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderMichael committed Feb 12, 2024
1 parent 68c643b commit 32e71f5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
Expand All @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Analyzer.Utilities.Lightup;
using Analyzer.Utilities.PooledObjects;

namespace Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines
Expand Down Expand Up @@ -54,7 +55,8 @@ private static void AnalyzeCompilation(CompilationAnalysisContext context)

IEnumerable<INamedTypeSymbol> globalTypes = context.Compilation.GlobalNamespace.GetTypeMembers().Where(item =>
Equals(item.ContainingAssembly, context.Compilation.Assembly) &&
MatchesConfiguredVisibility(item, context.Options, context.Compilation));
MatchesConfiguredVisibility(item, context.Options, context.Compilation) &&
!IsFileLocalWrapper.FromSymbol(item).IsFileLocal);

CheckTypeNames(globalTypes, context);
CheckNamespaceMembers(globalNamespaces, context);
Expand Down Expand Up @@ -96,8 +98,9 @@ private static void CheckNamespaceMembers(IEnumerable<INamespaceSymbol> namespac
{
// Get all the potentially externally visible types in the namespace
IEnumerable<INamedTypeSymbol> typeMembers = @namespace.GetTypeMembers().Where(item =>
Equals(item.ContainingAssembly, context.Compilation.Assembly) &&
MatchesConfiguredVisibility(item, context.Options, context.Compilation));
Equals(item.ContainingAssembly, context.Compilation.Assembly) &&
MatchesConfiguredVisibility(item, context.Options, context.Compilation) &&
!IsFileLocalWrapper.FromSymbol(item).IsFileLocal);

if (typeMembers.Any())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Linq;
Expand Down Expand Up @@ -142,6 +142,57 @@ public partial class F
}.RunAsync();
}

[Fact]
public async Task FileScopedTypesInNamespaceAsync()
{
string fileWithClass = """
namespace N;

file class C
{
}
""";

await new VerifyCS.Test
{
TestState =
{
Sources =
{
fileWithClass,
fileWithClass
}
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp11,
ReferenceAssemblies = ReferenceAssemblies.Net.Net70
}.RunAsync();
}

[Fact]
public async Task FileScopedTypesGlobalAsync()
{
string fileWithClass = """
file class C
{
}
""";

await new VerifyCS.Test
{
TestState =
{
Sources =
{
fileWithClass,
fileWithClass
}
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp11,
ReferenceAssemblies = ReferenceAssemblies.Net.Net70
}.RunAsync();
}


#endregion

#region Type Level
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Compiler/Analyzer.Utilities.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\WellKnownDiagnosticTagsExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Index.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IsExternalInit.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Lightup\IsFileLocalWrapper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Lightup\IOperationWrapper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Lightup\IFunctionPointerInvocationOperationWrapper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Lightup\IUtf8StringOperationWrapper.cs" />
Expand Down
35 changes: 35 additions & 0 deletions src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

#if HAS_IOPERATION

namespace Analyzer.Utilities.Lightup
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.CodeAnalysis;

[SuppressMessage("Performance", "CA1815:Override equals and operator equals on value types", Justification = "Not a comparable instance.")]
internal readonly struct IsFileLocalWrapper
{
private static readonly PropertyInfo? PropertyAccessor = typeof(INamedTypeSymbol).GetProperty("IsFileLocal", BindingFlags.Public | BindingFlags.Instance);

private static readonly Func<INamedTypeSymbol, bool> MemberAccessor = PropertyAccessor is null ? _ => false : symbol => (bool)PropertyAccessor.GetValue(symbol);

Check failure on line 17 in src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Debug)

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs#L17

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs(17,129): error CS8605: (NETCORE_ENGINEERING_TELEMETRY=Build) Unboxing a possibly null value.

Check failure on line 17 in src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Release)

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs#L17

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs(17,129): error CS8605: (NETCORE_ENGINEERING_TELEMETRY=Build) Unboxing a possibly null value.

Check failure on line 17 in src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs#L17

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs(17,129): error CS8605: (NETCORE_ENGINEERING_TELEMETRY=Build) Unboxing a possibly null value.

Check failure on line 17 in src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs#L17

src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs(17,129): error CS8605: (NETCORE_ENGINEERING_TELEMETRY=Build) Unboxing a possibly null value.

public INamedTypeSymbol WrappedSymbol { get; }

private IsFileLocalWrapper(INamedTypeSymbol symbol)
{
WrappedSymbol = symbol;
}

public static IsFileLocalWrapper FromSymbol(INamedTypeSymbol symbol)
{
return new IsFileLocalWrapper(symbol);
}

public bool IsFileLocal => MemberAccessor(WrappedSymbol);
}
}

#endif

0 comments on commit 32e71f5

Please sign in to comment.