dotnet/roslyn

Analyzer Improvement for IDE0059

Offen

#55.960 geöffnet am 27.08.2021

 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C# (4.257 Forks)batch import
Area-IDEFeature - IDE0059help wanted

Repository-Metriken

Stars
 (20.414 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

Brief description: The following two variable declarations are equivalent and only vary based on coding style:

a? ret;

if(Condition){
  ret = new a(){ ... };
} else {
  ret = new a(){ ... };
}

AND

var ret = default(a?);

if(Condition){
  ret = new a(){ ... };
} else {
  ret = new a(){ ... };
}

Today, IDE0059 triggers if a variable is initialized with any value and then later reassigned. I suggest updating IDE0059 to trigger only if a variable is initialized to a non-default value and then later reassigned.

Languages applicable: C# any maybe VB.

Code example that the analyzer SHOULD report: Needlessly initializing to a non-default value.

Code example that the analyzer not report: See above

Contributor Guide