dotnet / dotnet/roslyn

Add 'csharp_style_implicit_object_creation_in_return_statements' option for 'Simplify new expression (IDE0090)' analyzer

Open
#85,146 3 comments 0 reactions 0 assignees View on GitHub
Area-IDE Feature Request
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Brief description:** today, the ['Simplify new expression (IDE0090)' analyzer](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0090) only exposes the `csharp_style_implicit_object_creation_when_type_is_apparent` option. There's no way to make the analyzer also fire for return/yield statements/expressions.

We should add a new option (e.g. `csharp_style_implicit_object_creation_in_return_statements`) to support this.

This would fire the diagnostic for any `return` or `switch` statements/expressions that can be simplified as `new()`.

**Languages applicable:** C#

Is your analyzer C# only? VB Only? Or Both?

**Code example that the analyzer should report:**

```csharp
Foo M()
{
return new Foo(); // IDE0090, can simplify to 'return new();'
}

IEnumerable M()
{
yield return new Foo(); // IDE0090, can simplify to 'yield return new();'
}
```

Same for when using expressions as well.

**Documentation requirements:**

The analyzer already has docs ([here](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0090)), we'd just need to document the new option.

Contributor guide

Open the contributing guide

Research direction

Start with the IDE0090 analyzer implementation and its existing csharp_style_implicit_object_creation_when_type_is_apparent option. Trace how return, yield return, and switch statements or expressions are analyzed, then review the existing analyzer tests and the IDE0090 documentation page. Done means the new option controls the requested diagnostics and the documentation and tests cover the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers, documentation, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.