Skip to content

Commit 080ce01

Browse files
committed
Make MEN017 check for setter expression body
Fix #12
1 parent 5382a78 commit 080ce01

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<AssemblyOriginatorKeyFile>../Analyzers.snk</AssemblyOriginatorKeyFile>
2222

2323
<!-- NOTE: Change the version in Vsix\source.extension.vsixmanifest to match this! -->
24-
<Version>3.1.0</Version>
24+
<Version>3.1.1</Version>
2525
</PropertyGroup>
2626

2727
<PropertyGroup Condition="'$(Configuration)'=='Debug'">

src/Menees.Analyzers.Vsix/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
33
<Metadata>
4-
<Identity Id="Menees.Analyzers.Vsix" Version="3.1.0" Language="en-US" Publisher="Bill Menees"/>
4+
<Identity Id="Menees.Analyzers.Vsix" Version="3.1.1" Language="en-US" Publisher="Bill Menees"/>
55
<DisplayName>Menees.Analyzers.Vsix</DisplayName>
66
<Description xml:space="preserve">Provides analyzers for validating that tabs are used for indentation, that the lengths of lines, methods, properties, and files are acceptable, and that #regions are used within long files and files that contain multiple types.</Description>
77
<License>License.txt</License>

src/Menees.Analyzers/Men017RemoveUnusedPrivateSetter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ private static void HandleSetter(SyntaxNodeAnalysisContext context)
7878
&& propertyDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty // Can't change interfaces
7979
&& propertyDeclarationSymbol.SetMethod is IMethodSymbol setMethod
8080
&& setMethod.DeclaredAccessibility == Accessibility.Private
81-
&& accessorDeclaration.Body is null // Auto-property accessors have no body.
81+
&& accessorDeclaration.Body is null // Auto-property accessors have no block body.
82+
&& accessorDeclaration.ExpressionBody is null // Auto-property accessors have no expression body either.
8283
&& propertyDeclarationSymbol.GetAttributes().Length == 0 /* See Reflection note above*/)
8384
{
8485
bool canRemove = true;

tests/Menees.Analyzers.Test/Men017UnitTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,35 @@ public C(bool f)
330330
this.VerifyCSharpDiagnostic(Code);
331331
}
332332

333+
[TestMethod]
334+
public void PrimaryConstructorWithCustomSetter()
335+
{
336+
// https://github.com/menees/Analyzers/issues/12
337+
const string Code = """
338+
public sealed class MyClass
339+
{
340+
private readonly Dictionary<string, object?> myMap = new();
341+
342+
public MyClass(string myString) => this.MyString = myString;
343+
344+
public string MyString
345+
{
346+
get => this.Read<string>()!;
347+
private set => this.Write(value);
348+
}
349+
350+
[return: MaybeNull]
351+
private TValue Read<TValue>([CallerMemberName] string propertyName = "") =>
352+
this.myMap.TryGetValue(propertyName, out var value) ? (TValue)value! : default;
353+
354+
private void Write<TValue>([AllowNull] TValue value, [CallerMemberName] string propertyName = "") =>
355+
this.myMap[propertyName] = value;
356+
}
357+
""";
358+
359+
this.VerifyCSharpDiagnostic(Code);
360+
}
361+
333362
#endregion
334363

335364
#region Invalid Code Tests

0 commit comments

Comments
 (0)