From bbd6b47df1ee42762fbbb01f2ecb1f9a500e255d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 5 Jan 2026 18:40:46 +0000 Subject: [PATCH 1/4] Initial plan From e2f878a22f6ba396afec7e4ce47978b2bf57644d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 5 Jan 2026 18:44:51 +0000 Subject: [PATCH 2/4] Document IDE0059 known limitations in functions Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../code-analysis/style-rules/ide0059.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0059.md b/docs/fundamentals/code-analysis/style-rules/ide0059.md index 95b86af41e900..0c7c26983a71f 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0059.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0059.md @@ -55,6 +55,40 @@ You can take one of the following actions to fix this violation: int v = Compute2(); ``` +## Known limitations + +This rule doesn't currently work in certain code contexts. The analyzer fails to flag unnecessary value assignments in the following scenarios: + +- **Try/catch blocks**: Assignments inside `try` or `catch` blocks aren't detected. +- **Lambda expressions and delegates**: Variables assigned within lambda expressions or delegate bodies aren't flagged. +- **Using statements**: Assignments inside `using` statement blocks aren't detected. +- **Expression trees**: Variables in the presence of expression trees (`Expression>`) aren't flagged. + +For example, the following code contains unused assignments that IDE0059 doesn't detect: + +```csharp +public void Example() +{ + try + { + // This unused assignment isn't flagged by IDE0059 + var unused = new object(); + } + catch (Exception ex) + { + // Handle exception + } +} +``` + +These limitations are tracked in the following GitHub issues: + +- [IDE0059 is not always reported (Issue #52292)](https://github.com/dotnet/roslyn/issues/52292) +- [Detection of IDE0059 not working within try/catch statements (Issue #65220)](https://github.com/dotnet/roslyn/issues/65220) +- [IDE0059 not raised inside a code block with using keyword (Issue #74219)](https://github.com/dotnet/roslyn/issues/74219) +- [IDE0059 not working properly inside catch blocks (Issue #81742)](https://github.com/dotnet/roslyn/issues/81742) +- [IDE0059 doesn't work with expression trees (Issue #80033)](https://github.com/dotnet/roslyn/issues/80033) + ## Options The options for this specify whether to prefer the use of a discard or an unused local variable: From 70c828e3eb16ecad3bcb8ed2ce1a83cf92f436b0 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 5 Jan 2026 10:53:21 -0800 Subject: [PATCH 3/4] Human edits Updated the date for IDE0059 documentation and refined the known limitations section. --- .../code-analysis/style-rules/ide0059.md | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0059.md b/docs/fundamentals/code-analysis/style-rules/ide0059.md index 0c7c26983a71f..c1eb5318c31de 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0059.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0059.md @@ -1,7 +1,7 @@ --- title: "IDE0059" description: "Learn about code analysis style rule IDE0059." -ms.date: 12/13/2023 +ms.date: 01/05/2026 f1_keywords: - IDE0059 - csharp_style_unused_value_assignment_preference @@ -59,27 +59,10 @@ You can take one of the following actions to fix this violation: This rule doesn't currently work in certain code contexts. The analyzer fails to flag unnecessary value assignments in the following scenarios: -- **Try/catch blocks**: Assignments inside `try` or `catch` blocks aren't detected. -- **Lambda expressions and delegates**: Variables assigned within lambda expressions or delegate bodies aren't flagged. -- **Using statements**: Assignments inside `using` statement blocks aren't detected. -- **Expression trees**: Variables in the presence of expression trees (`Expression>`) aren't flagged. - -For example, the following code contains unused assignments that IDE0059 doesn't detect: - -```csharp -public void Example() -{ - try - { - // This unused assignment isn't flagged by IDE0059 - var unused = new object(); - } - catch (Exception ex) - { - // Handle exception - } -} -``` +- Assignments inside `try` or `catch` blocks. +- Assignments inside `using` statement blocks. +- Variables assigned within lambda expressions or delegate bodies. +- Variables in the presence of expression trees (`Expression>`). These limitations are tracked in the following GitHub issues: From cce71d11750c861ffbb7314601a86abe56607b39 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Wed, 7 Jan 2026 15:29:50 -0800 Subject: [PATCH 4/4] respond to feedback --- .../code-analysis/style-rules/ide0059.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0059.md b/docs/fundamentals/code-analysis/style-rules/ide0059.md index c1eb5318c31de..a4712a7f63340 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0059.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0059.md @@ -57,7 +57,7 @@ You can take one of the following actions to fix this violation: ## Known limitations -This rule doesn't currently work in certain code contexts. The analyzer fails to flag unnecessary value assignments in the following scenarios: +This rule doesn't work in certain code contexts. The analyzer fails to flag unnecessary value assignments in the following scenarios: - Assignments inside `try` or `catch` blocks. - Assignments inside `using` statement blocks. @@ -66,11 +66,11 @@ This rule doesn't currently work in certain code contexts. The analyzer fails to These limitations are tracked in the following GitHub issues: -- [IDE0059 is not always reported (Issue #52292)](https://github.com/dotnet/roslyn/issues/52292) -- [Detection of IDE0059 not working within try/catch statements (Issue #65220)](https://github.com/dotnet/roslyn/issues/65220) -- [IDE0059 not raised inside a code block with using keyword (Issue #74219)](https://github.com/dotnet/roslyn/issues/74219) -- [IDE0059 not working properly inside catch blocks (Issue #81742)](https://github.com/dotnet/roslyn/issues/81742) -- [IDE0059 doesn't work with expression trees (Issue #80033)](https://github.com/dotnet/roslyn/issues/80033) +- [IDE0059 is not always reported](https://github.com/dotnet/roslyn/issues/52292) +- [Detection of IDE0059 not working within try/catch statements](https://github.com/dotnet/roslyn/issues/65220) +- [IDE0059 not raised inside a code block with using keyword](https://github.com/dotnet/roslyn/issues/74219) +- [IDE0059 doesn't work with expression trees](https://github.com/dotnet/roslyn/issues/80033) +- [Remove unused variable/values (parent issue)](https://github.com/dotnet/roslyn/issues/75737) ## Options