Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsFileLocal lightup, use it in CA1708 #7190

Merged
merged 10 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -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,56 @@ 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
32 changes: 32 additions & 0 deletions src/Utilities/Compiler/Lightup/IsFileLocalWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 Microsoft.CodeAnalysis;

[SuppressMessage("Performance", "CA1815:Override equals and operator equals on value types", Justification = "Not a comparable instance.")]
internal readonly struct IsFileLocalWrapper
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
{
private static readonly Func<INamedTypeSymbol, bool> PropertyAccessor = LightupHelpers.CreateSymbolPropertyAccessor<INamedTypeSymbol, bool>(typeof(INamedTypeSymbol), nameof(IsFileLocal), fallbackResult: false);

public INamedTypeSymbol WrappedSymbol { get; }

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

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

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

#endif