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
5 changes: 5 additions & 0 deletions Source/Mockolate.Analyzers/Mockolate.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<InternalsVisibleTo Include="Mockolate.Analyzers.CodeFixers" PublicKey="$(PublicKey)"/>
<InternalsVisibleTo Include="Mockolate.Analyzers.Tests" PublicKey="$(PublicKey)"/>
</ItemGroup>

<ItemGroup>
Expand All @@ -40,4 +41,8 @@
</Compile>
</ItemGroup>

<ItemGroup>
<PackageReference Remove="Nullable"/>
</ItemGroup>

</Project>
71 changes: 71 additions & 0 deletions Source/Mockolate.Analyzers/Polyfills/NotNullWhenAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#region License
// MIT License
//
// Copyright (c) Manuel Römer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

#if !NULLABLE_ATTRIBUTES_DISABLE
#nullable enable
#pragma warning disable

namespace System.Diagnostics.CodeAnalysis
{
using global::System;

#if DEBUG
/// <summary>
/// Specifies that when a method returns <see cref="ReturnValue"/>,
/// the parameter will not be <see langword="null"/> even if the corresponding type allows it.
/// </summary>
#endif
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
#if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#endif
internal sealed class NotNullWhenAttribute : Attribute
{
#if DEBUG
/// <summary>
/// Gets the return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </summary>
#endif
public bool ReturnValue { get; }

#if DEBUG
/// <summary>
/// Initializes the attribute with the specified return value condition.
/// </summary>
/// <param name="returnValue">
/// The return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </param>
#endif
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}
Comment on lines +31 to +66
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

This file is indented with spaces, but the repo’s .editorconfig requires tab indentation for .cs files (indent_style = tab). Reformatting to tabs will keep formatting consistent and avoid editor/CI formatting churn.

Suggested change
using global::System;
#if DEBUG
/// <summary>
/// Specifies that when a method returns <see cref="ReturnValue"/>,
/// the parameter will not be <see langword="null"/> even if the corresponding type allows it.
/// </summary>
#endif
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
#if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#endif
internal sealed class NotNullWhenAttribute : Attribute
{
#if DEBUG
/// <summary>
/// Gets the return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </summary>
#endif
public bool ReturnValue { get; }
#if DEBUG
/// <summary>
/// Initializes the attribute with the specified return value condition.
/// </summary>
/// <param name="returnValue">
/// The return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </param>
#endif
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}
using global::System;
#if DEBUG
/// <summary>
/// Specifies that when a method returns <see cref="ReturnValue"/>,
/// the parameter will not be <see langword="null"/> even if the corresponding type allows it.
/// </summary>
#endif
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
#if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#endif
internal sealed class NotNullWhenAttribute : Attribute
{
#if DEBUG
/// <summary>
/// Gets the return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </summary>
#endif
public bool ReturnValue { get; }
#if DEBUG
/// <summary>
/// Initializes the attribute with the specified return value condition.
/// </summary>
/// <param name="returnValue">
/// The return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </param>
#endif
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}

Copilot uses AI. Check for mistakes.
}

#pragma warning restore
#nullable restore
#endif // NULLABLE_ATTRIBUTES_DISABLE
9 changes: 2 additions & 7 deletions Source/Mockolate.SourceGenerators/Entities/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,8 @@ public bool Equals(Method? x, Method? y)
}

// Compare parameters ignoring nullability annotations
MethodParameter[]? xParams = x.Parameters.AsArray();
MethodParameter[]? yParams = y.Parameters.AsArray();

if (xParams is null || yParams is null)
{
return xParams is null && yParams is null;
}
MethodParameter[] xParams = x.Parameters.AsArray()!;
MethodParameter[] yParams = y.Parameters.AsArray()!;

for (int i = 0; i < xParams.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<NoWarn>S3776</NoWarn>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Mockolate.SourceGenerators.Tests" PublicKey="$(PublicKey)"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all"/>
Expand All @@ -20,4 +24,8 @@
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
</ItemGroup>

<ItemGroup>
<PackageReference Remove="Nullable"/>
</ItemGroup>
Comment on lines +27 to +29
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

The PR description mentions adding test-project-local nullable-attribute polyfills and suppressing CS0436, but I don’t see any CS0436 suppression or test-side polyfill additions in this change set. If the collision is now resolved solely by removing the Nullable package from these source projects, consider updating the PR description to match (or add the missing changes if they’re still required).

Copilot uses AI. Check for mistakes.

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#region License
// MIT License
//
// Copyright (c) Manuel Römer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

#if !NULLABLE_ATTRIBUTES_DISABLE
#nullable enable
#pragma warning disable

namespace System.Diagnostics.CodeAnalysis
{
using global::System;

#if DEBUG
/// <summary>
/// Specifies that when a method returns <see cref="ReturnValue"/>,
/// the parameter will not be <see langword="null"/> even if the corresponding type allows it.
/// </summary>
#endif
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
#if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#endif
internal sealed class NotNullWhenAttribute : Attribute
{
#if DEBUG
/// <summary>
/// Gets the return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </summary>
#endif
public bool ReturnValue { get; }

#if DEBUG
/// <summary>
/// Initializes the attribute with the specified return value condition.
/// </summary>
/// <param name="returnValue">
/// The return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </param>
#endif
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}
Comment on lines +31 to +66
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

This file is indented with spaces, but the repo’s .editorconfig requires tab indentation for .cs files (indent_style = tab). Reformatting to tabs will keep formatting consistent and avoid editor/CI formatting churn.

Suggested change
using global::System;
#if DEBUG
/// <summary>
/// Specifies that when a method returns <see cref="ReturnValue"/>,
/// the parameter will not be <see langword="null"/> even if the corresponding type allows it.
/// </summary>
#endif
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
#if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#endif
internal sealed class NotNullWhenAttribute : Attribute
{
#if DEBUG
/// <summary>
/// Gets the return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </summary>
#endif
public bool ReturnValue { get; }
#if DEBUG
/// <summary>
/// Initializes the attribute with the specified return value condition.
/// </summary>
/// <param name="returnValue">
/// The return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </param>
#endif
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}
using global::System;
#if DEBUG
/// <summary>
/// Specifies that when a method returns <see cref="ReturnValue"/>,
/// the parameter will not be <see langword="null"/> even if the corresponding type allows it.
/// </summary>
#endif
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
#if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
#endif
internal sealed class NotNullWhenAttribute : Attribute
{
#if DEBUG
/// <summary>
/// Gets the return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </summary>
#endif
public bool ReturnValue { get; }
#if DEBUG
/// <summary>
/// Initializes the attribute with the specified return value condition.
/// </summary>
/// <param name="returnValue">
/// The return value condition.
/// If the method returns this value, the associated parameter will not be <see langword="null"/>.
/// </param>
#endif
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}

Copilot uses AI. Check for mistakes.
}

#pragma warning restore
#nullable restore
#endif // NULLABLE_ATTRIBUTES_DISABLE
123 changes: 123 additions & 0 deletions Tests/Mockolate.Analyzers.Tests/AnalyzerHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Linq;
using System.Threading.Tasks;
using aweXpect;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Xunit;
using static aweXpect.Expect;

namespace Mockolate.Analyzers.Tests;

public class AnalyzerHelpersTests
{
[Fact]
public async Task WhenInvokedMethodIsNotGeneric_ShouldNotReturnAnyTypeArgument()
{
Comment thread
vbreuss marked this conversation as resolved.
const string source = """
public class C
{
public void Foo() { }
public void Bar() { Foo(); }
}
""";
IMethodSymbol method = GetInvokedMethod(source, "Foo");

ITypeSymbol? result = AnalyzerHelpers.GetSingleInvocationTypeArgumentOrNull(method);

await That(result).IsNull();
}

[Fact]
public async Task WhenInvokedMethodIsGeneric_ShouldReturnFirstTypeArgument()
{
const string source = """
public class C
{
public T Foo<T>() => default!;
public void Bar() { Foo<int>(); }
}
""";
IMethodSymbol method = GetInvokedMethod(source, "Foo");

ITypeSymbol? result = AnalyzerHelpers.GetSingleInvocationTypeArgumentOrNull(method);

await That(result).IsNotNull();
await That(result!.SpecialType).IsEqualTo(SpecialType.System_Int32);
}

[Fact]
public async Task WhenSyntaxIsNotInvocationExpression_ShouldNotReturnAnyLocation()
{
const string source = """
public class C
{
public int Foo() => 0;
}
""";
SyntaxTree tree = CSharpSyntaxTree.ParseText(source);
CSharpCompilation compilation = CreateCompilation(tree);
SemanticModel model = compilation.GetSemanticModel(tree);
MethodDeclarationSyntax declaration = tree.GetRoot().DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.Single();
IMethodSymbol symbol = (IMethodSymbol)model.GetDeclaredSymbol(declaration)!;

Location? result = AnalyzerHelpers.GetTypeArgumentLocation(declaration, symbol.ReturnType);

await That(result).IsNull();
}

[Fact]
public async Task WhenInvocationHasGenericNameSyntax_ShouldReturnTypeArgumentLocation()
{
const string source = """
public static class S
{
public static T Make<T>() => default!;
}

public class C
{
public void Foo() { S.Make<int>(); }
}
""";
SyntaxTree tree = CSharpSyntaxTree.ParseText(source);
CSharpCompilation compilation = CreateCompilation(tree);
SemanticModel model = compilation.GetSemanticModel(tree);
InvocationExpressionSyntax invocation = tree.GetRoot().DescendantNodes()
.OfType<InvocationExpressionSyntax>()
.Single(i => i.Expression is MemberAccessExpressionSyntax { Name: GenericNameSyntax, });
IMethodSymbol method = (IMethodSymbol)model.GetSymbolInfo(invocation).Symbol!;
ITypeSymbol typeArgument = method.TypeArguments[0];

Location? result = AnalyzerHelpers.GetTypeArgumentLocation(invocation, typeArgument);

await That(result).IsNotNull();
}

private static IMethodSymbol GetInvokedMethod(string source, string methodName)
{
SyntaxTree tree = CSharpSyntaxTree.ParseText(source);
CSharpCompilation compilation = CreateCompilation(tree);
SemanticModel model = compilation.GetSemanticModel(tree);
InvocationExpressionSyntax invocation = tree.GetRoot().DescendantNodes()
.OfType<InvocationExpressionSyntax>()
.Single(i => InvocationName(i) == methodName);
return (IMethodSymbol)model.GetSymbolInfo(invocation).Symbol!;
}

private static string? InvocationName(InvocationExpressionSyntax invocation) => invocation.Expression switch
{
IdentifierNameSyntax id => id.Identifier.Text,
GenericNameSyntax generic => generic.Identifier.Text,
MemberAccessExpressionSyntax member => member.Name.Identifier.Text,
_ => null,
};

private static CSharpCompilation CreateCompilation(SyntaxTree tree) => CSharpCompilation.Create(
"TestAssembly",
[tree,],
[MetadataReference.CreateFromFile(typeof(object).Assembly.Location),],
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
}
Loading