Skip to content

Commit

Permalink
Make MEN017 check for setter expression body
Browse files Browse the repository at this point in the history
Fix #12
  • Loading branch information
menees committed Apr 11, 2024
1 parent 5382a78 commit 080ce01
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<AssemblyOriginatorKeyFile>../Analyzers.snk</AssemblyOriginatorKeyFile>

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

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Analyzers.Vsix/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<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">
<Metadata>
<Identity Id="Menees.Analyzers.Vsix" Version="3.1.0" Language="en-US" Publisher="Bill Menees"/>
<Identity Id="Menees.Analyzers.Vsix" Version="3.1.1" Language="en-US" Publisher="Bill Menees"/>
<DisplayName>Menees.Analyzers.Vsix</DisplayName>
<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>
<License>License.txt</License>
Expand Down
3 changes: 2 additions & 1 deletion src/Menees.Analyzers/Men017RemoveUnusedPrivateSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ private static void HandleSetter(SyntaxNodeAnalysisContext context)
&& propertyDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty // Can't change interfaces
&& propertyDeclarationSymbol.SetMethod is IMethodSymbol setMethod
&& setMethod.DeclaredAccessibility == Accessibility.Private
&& accessorDeclaration.Body is null // Auto-property accessors have no body.
&& accessorDeclaration.Body is null // Auto-property accessors have no block body.
&& accessorDeclaration.ExpressionBody is null // Auto-property accessors have no expression body either.
&& propertyDeclarationSymbol.GetAttributes().Length == 0 /* See Reflection note above*/)
{
bool canRemove = true;
Expand Down
29 changes: 29 additions & 0 deletions tests/Menees.Analyzers.Test/Men017UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,35 @@ public C(bool f)
this.VerifyCSharpDiagnostic(Code);
}

[TestMethod]
public void PrimaryConstructorWithCustomSetter()
{
// https://github.com/menees/Analyzers/issues/12
const string Code = """
public sealed class MyClass
{
private readonly Dictionary<string, object?> myMap = new();
public MyClass(string myString) => this.MyString = myString;
public string MyString
{
get => this.Read<string>()!;
private set => this.Write(value);
}
[return: MaybeNull]
private TValue Read<TValue>([CallerMemberName] string propertyName = "") =>
this.myMap.TryGetValue(propertyName, out var value) ? (TValue)value! : default;
private void Write<TValue>([AllowNull] TValue value, [CallerMemberName] string propertyName = "") =>
this.myMap[propertyName] = value;
}
""";

this.VerifyCSharpDiagnostic(Code);
}

#endregion

#region Invalid Code Tests
Expand Down

0 comments on commit 080ce01

Please sign in to comment.