dotnet / dotnet/roslynator

Refactoring request: nullable values to ?? syntax

Open
#1,507 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
3.5k
Forks
294
Avg merge
2h 30m
Merged PRs (30d)
4

Description

I would love to have some refactor tool that could suggest non-nullable variables from my parameters or any variable. So when the cursor is on the parameters, it can suggest to insert code:

**Actual Behavior**:
```csharp

MyMethod(int? intOrNull, string? strOrNull)
{
...
}
```

**Expected Behavior**:
```csharp
MyMethod(int? intOrNull, string? strOrNull)
{

// Suggested refactor: Convert null, to 0
Int intOrNullParam = intOrNullParam ?? 0;

// Suggested refactor: Convert null to empty string
string strOrNullParam= strOrNull ?? "";

...
}
```

**What would be the best solution here?**

Should also handle variables:
```csharp
bool? boolOrNull = null

// Suggested refactor: Convert bool to false
bool boolOrNullVar = boolOrNull ?? false
```

What would be the best solution here?

---
Similar issue:
https://github.com/dotnet/roslynator/issues/411

Contributor guide

Open the contributing guide

Research direction

No source file or test is named. Start by reviewing the related Roslynator issue 411 and the existing refactoring approach in this Roslyn-powered project, then define the supported nullable cases and replacement-value rules; done means the scope is agreed and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.