Skip to content

Commit

Permalink
Unused members analyzer: Do not flag write-only properties that are n…
Browse files Browse the repository at this point in the history
…ot read

Fixes #30894
  • Loading branch information
mavasani committed Nov 6, 2018
1 parent 443784e commit f9f6143
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,32 @@ class C : I
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedMembers)]
[WorkItem(30894, "https://github.com/dotnet/roslyn/issues/30894")]
public async Task WriteOnlyProperty_NotWritten()
{
await TestInRegularAndScriptAsync(
@"class C
{
int [|P|] { set { } }
}",
@"class C
{
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedMembers)]
[WorkItem(30894, "https://github.com/dotnet/roslyn/issues/30894")]
public async Task WriteOnlyProperty_Written()
{
await TestDiagnosticMissingAsync(
@"class C
{
int [|P|] { set { } }
void M(int i) => P = i;
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedMembers)]
public async Task FieldIsUnused_Const()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ private void OnSymbolEnd(SymbolAnalysisContext symbolEndContext, bool hasInvalid
? s_removeUnusedMembersRule
: s_removeUnreadMembersRule;

// Do not flag write-only properties that are not read.
if (rule == s_removeUnreadMembersRule &&
member is IPropertySymbol property &&
property.IsWriteOnly)
{
continue;
}

// Most of the members should have a single location, except for partial methods.
// We report the diagnostic on the first location of the member.
var diagnostic = Diagnostic.Create(
Expand Down

0 comments on commit f9f6143

Please sign in to comment.