提交 f9f6143d 编写于 作者: M Manish Vasani

Unused members analyzer: Do not flag write-only properties that are not read

Fixes #30894
上级 443784e3
......@@ -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()
{
......
......@@ -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(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册