Skip to content

Commit addebd4

Browse files
committed
CS8769, AllowNull and DisallowNull are being moved from a setter method to the owning propertry
1 parent 2d49eff commit addebd4

File tree

6 files changed

+278
-82
lines changed

6 files changed

+278
-82
lines changed

AutoInterfaceSample/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ public class Program
77
{
88
public static void Main()
99
{
10-
//System.Diagnostics.Debug.WriteLine(BeaKona.Output.Debug_TestRecord.Info);
10+
//System.Diagnostics.Debug.WriteLine(BeaKona.Output.Debug_TestDb.Info);
1111
}
1212
}
1313

1414
public class MyDb : IDb
1515
{
16+
[AllowNull]
1617
public string ConnectionString { get; [param: AllowNull] set; } = default!;
1718

19+
[AllowNull]
1820
public string this[int a, [AllowNull] string b]
1921
{
2022
get => b ?? "";
@@ -43,13 +45,15 @@ partial record TestDb([property: BeaKona.AutoInterface(typeof(IDb), IncludeBaseI
4345

4446
public interface IDb
4547
{
48+
[AllowNull]
4649
string ConnectionString
4750
{
4851
get;
4952
[param: AllowNull]
5053
set;
5154
}
5255

56+
[AllowNull]
5357
string this[int a, [AllowNull] string b]
5458
{
5559
get;

AutoInterfaceSampleNetStandard/TestRecord.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ partial class TestDb
1010

1111
public class MyDb : IDb
1212
{
13+
[AllowNull]
1314
public string ConnectionString { get; [param: AllowNull] set; } = default!;
1415

16+
[AllowNull]
1517
public string this[int a, [AllowNull] string b]
1618
{
1719
get => b ?? "";
@@ -24,13 +26,15 @@ public class MyDb : IDb
2426

2527
public interface IDb
2628
{
29+
[AllowNull]
2730
string ConnectionString
2831
{
2932
get;
3033
[param: AllowNull]
3134
set;
3235
}
3336

37+
[AllowNull]
3438
string this[int a, [AllowNull] string b]
3539
{
3640
get;
@@ -60,7 +64,7 @@ public NotNullWhenAttribute(bool returnValue)
6064
public bool ReturnValue { get; }
6165
}
6266

63-
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Class, Inherited = false)]
67+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, Inherited = false)]
6468
internal sealed class AllowNullAttribute : Attribute
6569
{
6670
public AllowNullAttribute()
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Collections.Immutable;
2+
3+
namespace BeaKona.AutoInterfaceGenerator;
4+
5+
internal readonly struct AttributeDataWithTarget(AttributeData attribute, string? target)
6+
{
7+
public AttributeData Attribute { get; } = attribute;
8+
public string? Target { get; } = target;
9+
10+
public static readonly IEqualityComparer<AttributeDataWithTarget> AttributeComparer = new SimpleAttributeComparer();
11+
private sealed class SimpleAttributeComparer : IEqualityComparer<AttributeDataWithTarget>
12+
{
13+
public bool Equals(AttributeDataWithTarget x, AttributeDataWithTarget y) => x.Attribute.Equals(y.Attribute);
14+
15+
public int GetHashCode(AttributeDataWithTarget obj) => obj.Attribute.GetHashCode();
16+
}
17+
18+
public static readonly IEqualityComparer<AttributeDataWithTarget> DefaultComparer = new DefaultComparer2();
19+
private sealed class DefaultComparer2 : IEqualityComparer<AttributeDataWithTarget>
20+
{
21+
public bool Equals(AttributeDataWithTarget x, AttributeDataWithTarget y) => AttributeDataComparer.Equals(x.Attribute, y.Attribute) && Helpers.EqualStrings(x.Target, y.Target);
22+
23+
public int GetHashCode(AttributeDataWithTarget obj)
24+
{
25+
return SymbolEqualityComparer.Default.GetHashCode(obj.Attribute.AttributeClass) + (obj.Target?.GetHashCode() ?? 0);
26+
}
27+
}
28+
29+
private static readonly IEqualityComparer<TypedConstant> TypedConstantComparer = new TypedConstantComparer2();
30+
private sealed class TypedConstantComparer2 : IEqualityComparer<TypedConstant>
31+
{
32+
public bool Equals(TypedConstant x, TypedConstant y)
33+
{
34+
if (x.Kind != y.Kind)
35+
{
36+
return false;
37+
}
38+
39+
if (x.IsNull)
40+
{
41+
if (y.IsNull)
42+
{
43+
return true;
44+
}
45+
else
46+
{
47+
return false;
48+
}
49+
}
50+
else
51+
{
52+
if (y.IsNull)
53+
{
54+
return false;
55+
}
56+
else
57+
{
58+
if (x.Value is ISymbol xs && y.Value is ISymbol ys)
59+
{
60+
return SymbolEqualityComparer.Default.Equals(xs, ys);
61+
}
62+
else
63+
{
64+
return object.Equals(x.Value, y.Value);
65+
}
66+
}
67+
}
68+
}
69+
70+
public int GetHashCode(TypedConstant obj)
71+
{
72+
return unchecked(obj.Kind.GetHashCode() + (obj.IsNull ? 0 : (obj.Value?.GetHashCode() ?? 0)));
73+
}
74+
}
75+
76+
public static readonly IEqualityComparer<AttributeData> AttributeDataComparer = new AttributeDataComparer2();
77+
78+
private sealed class AttributeDataComparer2 : IEqualityComparer<AttributeData>
79+
{
80+
public bool Equals(AttributeData x, AttributeData y)
81+
{
82+
if (SymbolEqualityComparer.Default.Equals(x.AttributeClass, y.AttributeClass) == false)
83+
{
84+
return false;
85+
}
86+
87+
if (Helpers.EqualCollections(x.ConstructorArguments, y.ConstructorArguments, TypedConstantComparer) == false)
88+
{
89+
return false;
90+
}
91+
92+
if (Helpers.EqualDictionaries(x.NamedArguments.ToImmutableDictionary(i => i.Key, i => i.Value), y.NamedArguments.ToImmutableDictionary(i => i.Key, i => i.Value), TypedConstantComparer) == false)
93+
{
94+
return false;
95+
}
96+
97+
return true;
98+
}
99+
100+
public int GetHashCode(AttributeData obj)
101+
{
102+
return SymbolEqualityComparer.Default.GetHashCode(obj.AttributeClass);
103+
}
104+
}
105+
}

BeaKona.AutoInterfaceGenerator/BeaKona.AutoInterfaceGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<RepositoryUrl>https://github.com/beakona/AutoInterface</RepositoryUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>
16-
<Version>1.0.42</Version>
16+
<Version>1.0.43</Version>
1717
<IsRoslynComponent>true</IsRoslynComponent>
1818
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
1919
</PropertyGroup>

0 commit comments

Comments
 (0)