Skip to content

Latest commit

 

History

History
2173 lines (1513 loc) · 49.4 KB

Refactorings.md

File metadata and controls

2173 lines (1513 loc) · 49.4 KB

Roslynator Refactorings

Add braces (RR0002)

  • Syntax: do statement, else clause, fixed statement, for statement, foreach statement, if statement, lock statement, using statement, while statement
  • Span: embedded statement Add braces

Add braces to if-else (RR0003)

  • Syntax: if-else chain
  • Span: embedded statement Add braces to if-else

Add braces to switch section (RR0004)

  • Syntax: switch section
  • Span: case or default keyword Add braces to switch section

Add braces to switch sections (RR0005)

  • Syntax: switch statement
  • Span: case or default keyword Add braces to switch sections

Add cast expression (RR0006)

  • Syntax: argument, assignment expression, return statement, variable declaration Add cast expression

Add cast expression

Add cast expression

Add cast expression

Add default value to parameter (RR0007)

  • Syntax: parameter without default value
  • Span: identifier Add default value to parameter

Add default value to return statement (RR0008)

  • Syntax: return statement without expression Add default value to return statement

Add empty line between declarations (RR0205)

  • Syntax: selected declarations

Before

private object x;
private object y;
private object z;

After

private object x;

private object y;

private object z;

Add exception to documentation comment (RR0009)

  • Syntax: throw statement Add exception to documentation comment

Add identifier to parameter (RR0012)

  • Syntax: parameter
  • Span: missing identifier Add identifier to parameter

Add identifier to variable declaration (RR0010)

  • Syntax: variable declaration Add identifier to variable declaration

Add member to interface (RR0195)

  • Syntax: method, property, indexer, event
  • Span: identifier

Before

public class Foo : IFoo
{
    public void Bar()
    {
    }
}

public interface IFoo
{
}

After

public class Foo : IFoo
{
    public void Bar()
    {
    }
}

public interface IFoo
{
    void Bar();
}

Add missing cases to switch statement (RR0059)

  • Syntax: switch statement

Before

switch (dayOfWeek)
{
    case DayOfWeek.Sunday:
        break;
    case DayOfWeek.Monday:
        break;
    case DayOfWeek.Tuesday:
        break;
    case DayOfWeek.Wednesday:
        break;
    case DayOfWeek.Thursday:
        break;
}

After

switch (dayOfWeek)
{
    case DayOfWeek.Sunday:
        break;
    case DayOfWeek.Monday:
        break;
    case DayOfWeek.Tuesday:
        break;
    case DayOfWeek.Wednesday:
        break;
    case DayOfWeek.Thursday:
        break;
    case DayOfWeek.Friday:
        break;
    case DayOfWeek.Saturday:
        break;
}

Add parameter name to argument (RR0011)

  • Syntax: argument list Add parameter name to argument

Add tag to documentation comment (RR0208)

  • Syntax: selected word(s) in documentation comment

Before

/// <summary>
/// null
/// </summary>
public class Foo
{
}

After

/// <summary>
/// <c>null</c>
/// </summary>
public class Foo
{
}

Add type parameter (RR0178)

  • Syntax: class declaration, struct declaration, interface declaration, delegate declaration, method declaration, local function Add type parameter

Add using directive (RR0013)

  • Syntax: qualified name
  • Span: selected namespace Add using directive

Add using static directive (RR0014)

  • Syntax: member access expression (public or internal static class)
  • Span: selected class name Add using static directive

Call 'ConfigureAwait(false)' (RR0015)

  • Syntax: awaitable method invocation
  • Span: method name Call 'ConfigureAwait(false)'

Call extension method as instance method (RR0016)

  • Syntax: method invocation Call extension method as instance method

Call string.IndexOf instead of string.Contains (RR0144)

  • Syntax: method invocation
  • Span: method name

Before

if (s.Contains("a"))
{
{

After

if (s.IndexOf("a", StringComparison.OrdinalIgnoreCase) != -1)
{
{

Call 'To...' method (ToString, ToArray, ToList) (RR0017)

  • Syntax: argument, assignment expression, return statement, variable declaration Call 'To...' method (ToString, ToArray, ToList)

Change accessibility (RR0186)

  • Syntax: access modifier Change accessibility

Change explicit type to 'var' (RR0018)

  • Syntax: variable declaration, foreach statement
  • Span: type Change explicit type to 'var'

Change method return type to 'void' (RR0021)

  • Syntax: method, local function Change method return type to 'void'

Change method/property/indexer type according to return expression (RR0019)

  • Syntax: return statement in method/property/indexer Change method/property/indexer type according to return expression

Change method/property/indexer type according to yield return expression (RR0020)

  • Syntax: yield return statement in method/property/indexer Change method/property/indexer type according to yield return expression

Change type according to expression (RR0022)

  • Syntax: variable declaration, foreach statement
  • Span: type

Before

IEnumerable<object> items = new List<object>();

After

List<object> items = new List<object>();

Change 'var' to explicit type (RR0023)

  • Syntax: variable declaration, foreach statetement
  • Span: type Change 'var' to explicit type

Check expression for null (RR0024)

  • Syntax: local declaration (identifier), assignment expression (left) Check expression for null

Check parameter for null (RR0025)

  • Syntax: parameter
  • Span: parameter identifier Check parameter for null

Collapse to initalizer (RR0026)

  • Syntax: object creation followed with assignment(s) Collapse to initalizer

Comment out member (RR0027)

  • Syntax: method, constructor, property, indexer, operator, event, namespace, class, struct, interface
  • Span: opening or closing brace Comment out member

Comment out statement (RR0028)

  • Syntax: do statement, fixed statement, for statement, foreach statement, checked statement, if statement, lock statement, switch statement, try statement, unchecked statement, unsafe statement, using statement, while statement
  • Span: opening or closing brace Comment out statement

Convert statements to if-else (RR0211)

  • Syntax: selected statements (first statement must be 'if' statement)

Before

if (x)
    return 1;

if (y)
{
    return 2;
}
else if (z)
{
    return 3;
}

return 0;

After

if (x)
{
    return 1;
}
else if (y)
{
    return 2;
}
else if (z)
{
    return 3;
}
else
{
    return 0;
}

Copy documentation comment from base member (RR0029)

  • Syntax: constructor, method, property, indexer, event Copy documentation comment from base member

Copy documentation comment from base member

Duplicate argument (RR0030)

  • Syntax: missing argument Duplicate argument

Duplicate member (RR0031)

  • Syntax: method, constructor, property, indexer, operator, event, namespace, class, struct, interface
  • Span: opening or closing brace Duplicate member

Duplicate parameter (RR0032)

  • Syntax: missing parameter Duplicate parameter

Duplicate statement (RR0033)

  • Syntax: do statement, fixed statement, for statement, foreach statement, checked statement, if statement, lock statement, switch statement, try statement, unchecked statement, unsafe statement, using statement, while statement
  • Span: opening or closing brace Duplicate statement

Expand coalesce expression (RR0035)

  • Syntax: coalesce expression
  • Span: ?? operator Expand coalesce expression

Expand compound assignment operator (RR0034)

  • Syntax: compound assignment expression
  • Span: operator Expand compound assignment operator

Expand event (RR0036)

  • Syntax: event field declaration Expand event

Expand expression body (RR0037)

  • Syntax: expression body Expand expression body

Expand initializer (RR0038)

  • Syntax: initializer Expand initializer

Expand lambda expression body (RR0039)

  • Syntax: lambda expression
  • Span: body Expand lambda expression body

Expand property (RR0040)

  • Syntax: auto-property Expand property

Expand property and add backing field (RR0041)

  • Syntax: auto-property Expand property and add backing field

Extract event handler method (RR0203)

  • Syntax: lambda expression

Before

void Foo()
{
  x.Changed += (s, e) => Bar();
}

After

void Foo()
{
  x.Changed += Changed;
}

void OnChanged(object sender, EventArgs e)
{
  Bar();
}

Extract expression from condition (RR0043)

  • Syntax: if statement, while statement
  • Span: condition

Before

if (x && y) // Select 'y'
{
}

After

if(x)
{
    if (y)
    {
    }
}

Before

if (x || y) // Select 'y'
{
}

After

if(x)
{
}

if (y)
{
}

Extract generic type (RR0044)

  • Syntax: generic name with single type argument
  • Span: type argument Extract generic type

Extract statement(s) (RR0045)

  • Syntax: else clause, fixed statement, for statement, foreach statement, checked statement, if statement, lock statement, try statement, unsafe statement, using statement, while statement Extract statement(s)

Extract type declaration to a new file (RR0046)

  • Syntax: class declaration, struct declaration, interface declaration, enum declaration, delegate declaration
  • Span: identifier Extract type declaration to a new file

Format accessor braces (RR0047)

  • Syntax: get accessor, set accessor, add accessor, remove accessor
  • Span: block Format accessor braces

Format accessor braces

Format argument list (RR0048)

  • Syntax: argument list Format argument list

Format argument list

Format binary expression (RR0049)

  • Syntax: logical and/or expression, bitwise and/or expression Format binary expression

Format conditional expression (RR0050)

  • Syntax: conditional expression Format conditional expression

Format conditional expression

Format constraint clauses (RR0187)

  • Syntax: type parameter constraint clause

Before

private void Foo<T1, T2, T3>() where T1 : class where T2 : class where T3 : class
{
}

After

private void Foo<T1, T2, T3>()
    where T1 : class
    where T2 : class
    where T3 : class
{
}

Format expression chain (RR0051)

  • Syntax: expression chain Format expression chain

Format expression chain

Format initializer (RR0052)

  • Syntax: initializer Format initializer

Format initializer

Format parameter list (RR0053)

  • Syntax: parameter list Format parameter list

Format parameter list

Generate base constructors (RR0054)

  • Syntax: class declaration
  • Span: identifier Generate base constructors

Generate combined enum member (RR0055)

  • Syntax: enum declaration (with FlagsAttribute) Generate combined enum member

Generate enum member (RR0056)

  • Syntax: enum declaration (with FlagsAttribute) Generate enum member

Generate enum values (RR0057)

  • Syntax: enum declaration (with FlagsAttribute) Generate enum values

Generate event invoking method (RR0058)

  • Syntax: event
  • Span: identifier Generate event invoking method

Generate property for DebuggerDisplay attribute (RR0204)

  • Syntax: DebuggerDisplay attribute

Before

[DebuggerDisplay("A: {A} B: {B}")]
public class Foo
{
    public string A { get; }
    public string B { get; }
}

After

DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Foo
{
    public string A { get; }
    public string B { get; }

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string DebuggerDisplay
    {
        get { return $"A: {A} B: {B}"; }
    }
}

Implement custom enumerator (RR0210)

  • Syntax: class that implements IEnumerable<T>
  • Span: identifier

Before

using System;
using System.Collections;
using System.Collections.Generic;

class C<T> : IEnumerable<T>
{
    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

After

using System;
using System.Collections;
using System.Collections.Generic;

class C<T> : IEnumerable<T>
{
    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    public Enumerator GetEnumerator()
    {
        return new Enumerator(this);
    }

    public struct Enumerator
    {
        private readonly C<T> _c;
        private int _index;

        internal Enumerator(C<T> c)
        {
            _c = c;
            _index = -1;
        }

        public T Current
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public bool MoveNext()
        {
            throw new NotImplementedException();
        }

        public void Reset()
        {
            _index = -1;
            throw new NotImplementedException();
        }

        public override bool Equals(object obj)
        {
            throw new NotSupportedException();
        }

        public override int GetHashCode()
        {
            throw new NotSupportedException();
        }
    }

    //TODO: IEnumerable.GetEnumerator() and IEnumerable<T>.GetEnumerator() should return instance of EnumeratorImpl.
    private class EnumeratorImpl : IEnumerator<T>
    {
        private Enumerator _e;

        internal EnumeratorImpl(C<T> c)
        {
            _e = new Enumerator(c);
        }

        public T Current
        {
            get
            {
                return _e.Current;
            }
        }

        object IEnumerator.Current
        {
            get
            {
                return _e.Current;
            }
        }

        public bool MoveNext()
        {
            return _e.MoveNext();
        }

        void IEnumerator.Reset()
        {
            _e.Reset();
        }

        void IDisposable.Dispose()
        {
        }
    }
}

Implement IEquatable<T> (RR0179)

  • Syntax: class declaration, struct declaration, interface declaration
  • Span: base list Implement IEquatable<T>

Initialize field from constructor (RR0197)

  • Syntax: field declaration
  • Span: idenifier

Before

public class Foo
{
    private string _bar;

    public Foo()
    {
    }

    public Foo(object parameter)
    {
    }

    public Foo(object parameter1, object parameter2)
        : this(parameter1)
    {
    }
}

After

public class Foo
{
    private string _bar;

    public Foo(string bar)
    {
        _bar = bar;
    }

    public Foo(object parameter, string bar)
    {
        _bar = bar;
    }

    public Foo(object parameter1, object parameter2, string bar)
        : this(parameter1, bar)
    {
        _bar = bar;
    }
}

Initialize local with default value (RR0060)

  • Syntax: local declaration without initializer
  • Span: identifier Initialize local with default value

Inline alias expression (RR0061)

  • Syntax: using alias directive
  • Span: identifier Inline alias expression

Inline constant (RR0181)

  • Syntax: constant declaration Inline constant

Inline constant value (RR0127)

  • Syntax: expression that has constant value

Before

public const string Value = "x";

void Foo()
{
    string x = Value;
}

After

public const string Value = "x";

void Foo()
{
    string x = "x";
}

Inline method (RR0062)

  • Syntax: method invocation Inline method

Inline property (RR0198)

  • Syntax: property access Inline property

Inline using static (RR0180)

  • Syntax: using static directive Inline using static

Insert string interpolation (RR0063)

  • Syntax: string literal, interpolated string Insert string interpolation

Insert string interpolation

Introduce and initialize field (RR0064)

  • Syntax: constructor parameter Introduce and initialize field

Introduce and initialize property (RR0065)

  • Syntax: constructor parameter Introduce and initialize property

Introduce constructor (RR0066)

  • Syntax: field, property Introduce constructor

Introduce field to lock on (RR0067)

  • Syntax: lock statement
  • Span: missing expression Introduce field to lock on

Introduce local variable (RR0068)

  • Syntax: expression statement, expression in using statement Introduce local variable

Invert binary expression (RR0079)

  • Syntax: logical and/or expression Invert binary expression

Invert boolean literal (RR0080)

  • Syntax: boolean literal Invert boolean literal

Invert conditional expression (RR0160)

  • Syntax: conditional expression
  • Span: condition Invert conditional expression

Invert if (RR0189)

  • Syntax: if statement
  • Span: if keyword

Before

if (condition1)
{
    if (condition2)
    {
        Foo();
    }
}

After

if (!condition1)
{
    return;
}

if (!condition2)
{
    return;
}

Foo();

Before

if (!condition1)
{
    return;
}

if (!condition2)
{
    return;
}

Foo();

After

if (condition1)
{
    if (condition2)
    {
        Foo();
    }
}

Invert if-else (RR0162)

  • Syntax: if-else statement
  • Span: if keyword Invert if-else

Invert is expression (RR0081)

  • Syntax: is expression
  • Span: operator Invert is expression

Invert operator (RR0082)

  • Syntax: !=, &&, ||, <, <=, ==, >, >= Invert operator

Invert prefix/postfix unary operator (RR0134)

  • Syntax: prefix/postfix unary expression
  • Span: operator token

Before

int i = 0;

i++;

After

int i = 0;

i--;

Before

int i = 0;

++i;

After

int i = 0;

--i;

Join string expressions (RR0078)

  • Syntax: concatenated string expressions Join string expressions

Join string expressions

Join string expressions

Make member abstract (RR0069)

  • Syntax: non-abstract indexer/method/property in abstract class
  • Span: indexer/method/property header Make member abstract

Make member virtual (RR0070)

  • Syntax: method declaration, indexer declaration Make member virtual

Merge assignment expression with return statement (RR0073)

  • Syntax: assignment expression followed with return statement Merge assignment expression with return statement

Merge attributes (RR0074)

  • Syntax: selected attribute lists Merge attributes

Merge if statements (RR0075)

  • Syntax: selected if statements

Before

bool condition1 = false;
bool condition2 = false;

if (condition1)
{
    return false;
}

if (condition2)
{
    return false;
}

return true;

After

bool condition1 = false;
bool condition2 = false;

if (condition1 || condition2)
{
    return false;
}

return true;

Merge if with parent if (RR0196)

  • Syntax: if statement
  • Span: if keyword

Before

if (x)
{
    if (y)
    {
    }
}
else
{
}

After

if (x && y)
{
}
else
{
}

Merge interpolation into interpolated string (RR0076)

  • Syntax: interpolation Merge interpolation into interpolated string

Merge local declarations (RR0077)

  • Syntax: local declarations with same type Merge local declarations

Move unsafe context to containing declaration (RR0202)

  • Syntax: unsafe declaration
  • Span: unsafe modifier

Before

public class Foo
{
  public unsafe void Bar()
  {
  }
}

After

public unsafe class Foo
{
  public void Bar()
  {
  }
}

Notify property changed (RR0083)

  • Syntax: property in class/struct that implements INotifyPropertyChanged
  • Span: setter Notify property changed

Parenthesize expression (RR0084)

  • Syntax: selected expression Parenthesize expression

Promote local to parameter (RR0085)

  • Syntax: local declaration in method Promote local to parameter

Remove all comments (RR0086)

  • Syntax: singleline/multiline comment, singleline/multiline documentation documentation comment Remove all comments

Remove all comments (except documentation comments) (RR0087)

  • Syntax: singleline/multiline comment Remove all comments (except documentation comments)

Remove all documentation comments (RR0088)

  • Syntax: singleline/multiline documentation comment Remove all documentation comments

Remove all member declarations (RR0089)

  • Syntax: namespace, class, struct, interface
  • Span: opening or closing brace Remove all member declarations

Remove all preprocessor directives (RR0090)

  • Syntax: preprocessor directive Remove all preprocessor directives

Remove all region directives (RR0091)

  • Syntax: region directive Remove all region directives

Remove all statements (RR0092)

  • Syntax: method, constructor, operator
  • Span: opening or closing brace Remove all statements

Remove all switch sections (RR0093)

  • Syntax: switch statement
  • Span: opening or closing brace Remove all switch sections

Remove async/await (RR0209)

  • Syntax: method declaration, local function, lambda, anonymous method
  • Span: async keyword

Before

class C
{
    async Task<object> FooAsync()
    {
        return await BarAsync().ConfigureAwait(false);
    }
}

After

class C
{
    Task<object> FooAsync()
    {
        return BarAsync();
    }
}

Remove braces (RR0094)

  • Syntax: do statement, else clause, fixed statement, for statement, foreach statement, if statement, lock statement, using statement, while statement
  • Span: block with a single statement Remove braces

Remove braces from if-else (RR0095)

  • Syntax: if-else chain
  • Span: embedded statement Remove braces from if-else

Remove braces from switch section (RR0096)

  • Syntax: switch section
  • Span: case or default keyword Remove braces from switch section

Remove braces from switch sections (RR0097)

  • Syntax: switch statement
  • Span: case or default keyword Remove braces from switch sections

Remove comment (RR0098)

  • Syntax: singleline/multiline comment, singleline/multiline xml documentation comment Remove comment

Remove condition from last else clause (RR0099)

  • Syntax: else clause
  • Span: else keyword Remove condition from last else clause

Remove directive and related directives (RR0100)

  • Syntax: preprocessor directive, region directive Remove directive and related directives

Remove empty lines (RR0101)

  • Syntax: selected lines Remove empty lines

Remove enum member value(s) (RR0199)

  • Syntax: selected enum member(s)

Before

public enum Foo
{
    One = 1,
    Two = 2,
    Three = 3
}

After

public enum Foo
{
    One,
    Two,
    Three
}

Remove interpolation (RR0102)

  • Syntax: string interpolation
  • Span: opening or closing brace Remove interpolation

Remove member (RR0103)

  • Syntax: method, constructor, property, indexer, operator, event, namespace, class, struct, interface
  • Span: opening or closing brace Remove member

Remove member declarations above/below (RR0104)

  • Syntax: empty line between member declarations Remove member declarations above/below

Remove parameter name from argument (RR0105)

  • Syntax: selected argument(s) Remove parameter name from argument

Remove parentheses (RR0106)

  • Syntax: parenthesized expression
  • Span: opening or closing parenthesis Remove parentheses

Remove property initializer (RR0107)

  • Syntax: property initializer Remove property initializer

Remove region (RR0108)

  • Syntax: region directive Remove region

Remove statement (RR0109)

  • Syntax: do statement, fixed statement, for statement, foreach statement, checked statement, if statement, lock statement, switch statement, try statement, unchecked statement, unsafe statement, using statement, while statement
  • Span: open/close brace Remove statement

Remove statements from switch sections (RR0110)

  • Syntax: selected switch sections Remove statements from switch sections

Rename backing field according to property name (RR0111)

  • Syntax: field identifier inside property declaration Rename backing field according to property name

Rename identifier according to type name (RR0112)

  • Syntax: foreach statement, local/field/constant declaration
  • Span: identifier Rename identifier according to type name

Rename identifier according to type name

Rename method according to type name (RR0113)

  • Syntax: method Rename method according to type name

Rename parameter according to its type name (RR0114)

  • Syntax: parameter
  • Span: parameter identifier Rename parameter according to its type name

Rename property according to type name (RR0115)

  • Syntax: property identifier Rename property according to type name

Replace (yield) return statement with if-else (RR0143)

  • Syntax: return statement, yield return statement
  • Span: selected statement, yield keyword or return keyword Replace (yield) return statement with if-else

Replace ?: with if-else (RR0120)

  • Syntax: local declaration statement with conditional expression, assignment with conditional expression, return statement conditional expression, yield statement conditional expression

Before

string s = (x) ? "a" : "b";

After

string s;
if (x)
{
    s = "a";
}
else
{
    s = "b";
}

Before

string s = (x) ? "a" : (y) ? "b" : "c";

After

string s;
if (x)
{
    s = "a";
}
else if (y)
{
    s = "b";
}
else
{
    s = "c";
}

Replace Any with All (or All with Any) (RR0116)

  • Syntax: Any(Func<T, bool> or All(Func<T, bool> from System.Linq.Enumerable namespace
  • Span: method name Replace Any with All (or All with Any)

Replace as expression with cast expression (RR0117)

  • Syntax: as expression Replace as expression with cast expression

Replace cast expression with as expression (RR0118)

  • Syntax: cast expression Replace cast expression with as expression

Replace comment with documentation comment (RR0192)

  • Syntax: single-line comment

Before

// comment
public class Foo
{
}

After

/// <summary>
/// comment
/// </summary>
public class Foo
{
}

Replace conditional expression with expression (RR0119)

  • Syntax: conditional expression
  • Span: selected true/false expression Replace conditional expression with expression

Replace constant with field (RR0121)

  • Syntax: constant declaration Replace constant with field

Replace do statement with while statement (RR0123)

  • Syntax: do statement
  • Span: do keyword

Before

do
{
} while (condition);

After

while (condition)
{
}

Replace equals expression with string.Equals (RR0124)

  • Syntax: equals expression, not equals expression
  • Span: operator Replace equals expression with string.Equals

Replace equals expression with string.IsNullOrEmpty (RR0125)

  • Syntax: equals expression, not equals expression
  • Span: operator Replace equals expression with string.IsNullOrEmpty

Replace equals expression with string.IsNullOrWhiteSpace (RR0126)

  • Syntax: equals expression, not equals expression
  • Span: operator Replace equals expression with string.IsNullOrWhiteSpace

Replace for statement with foreach statement (RR0130)

  • Syntax: for statement Replace for statement with foreach statement

Replace for statement with while statement (RR0131)

  • Syntax: for statement
  • Span: for keyword or selected for statement Replace for statement with while statement

Replace foreach statement with for statement (RR0129)

  • Syntax: foreach statement Replace foreach statement with for statement

Replace foreach with enumerator (RR0206)

  • Syntax: foreach statement
  • Span: foreach keyword

Before

foreach (var item in items)
{
    yield return item;
}

After

using (var en = items.GetEnumerator())
{
    while (en.MoveNext())
    {
        yield return item;
    }
}

Replace foreach with for and reverse loop (RR0188)

  • Syntax: foreach statement

Before

foreach (object item in items)
{
    yield return item;
}

After

for (int i = items.Count - 1; i >= 0; i--)
{
    yield return items[i];
}

Replace hexadecimal literal with decimal literal (RR0132)

  • Syntax: hexadecimal literal Replace hexadecimal literal with decimal literal

Replace if with switch (RR0133)

  • Syntax: if statement
  • Span: top if keyword or selected if statement

Before

var ch = stringReader.Read();

if (ch == 10 || ch == 13)
{
    return;
}
else
{
    stringBuilder.Append(ch);
}

After

var ch = stringReader.Read();

switch (ch)
{
    case 10:
    case 13:
        {
            return;
        }

    default:
        {
            stringBuilder.Append(ch);
            break;
        }
}

Replace interpolated string with concatenation (RR0193)

  • Syntax: interpolated string

Before

string s = $"a{b}c";

After

string s = "a" + b + "c";

Replace interpolated string with interpolation expression (RR0135)

  • Syntax: interpolated string with single interpolation and no text
  • Span: interpolation Replace interpolated string with interpolation expression

Replace interpolated string with string literal (RR0136)

  • Syntax: Interpolated string without any interpolation Replace interpolated string with string literal

Replace interpolated string with string.Format (RR0201)

  • Syntax: interpolated string

Before

$"name: {name,0:f}, value: {value}"

After

string.Format("name: {0,0:f} value: {1}", name, value)

Replace method group with lambda (RR0137)

  • Syntax: method group

Before

Func<object, object, object> func = Foo;

After

Func<object, object, object> func = (f, g) => Foo(f, g)

Replace method with property (RR0138)

  • Syntax: method
  • Span: method header Replace method with property

Replace null literal expression with default expression (RR0139)

  • Syntax: argument Replace null literal expression with default expression

Replace object creation with default value (RR0185)

  • Syntax: object creation expression

Before

var x = new object();

After

object x = null;

Before

var arr = new object[0];

After

object[] arr = null;

Replace prefix operator to postfix operator (RR0140)

  • Syntax: prefix/postfix unary expression Replace prefix operator to postfix operator

Replace property with method (RR0141)

  • Syntax: read-only property
  • Span: property header Replace property with method

Replace regular string literal with verbatim string literal (RR0142)

  • Syntax: regular string literal Replace regular string literal with verbatim string literal

Replace string.Format with interpolated string (RR0145)

  • Syntax: string.Format method Replace string.Format with interpolated string

Replace switch with if (RR0147)

  • Syntax: switch statement
  • Span: switch keyword Replace switch with if

Replace verbatim string literal with regular string literal (RR0148)

  • Syntax: verbatim string literal Replace verbatim string literal with regular string literal

Replace verbatim string literal with regular string literals (RR0149)

  • Syntax: multiline verbatim string literal Replace verbatim string literal with regular string literals

Replace while statement with do statement (RR0150)

  • Syntax: while statement
  • Span: while keyword

Before

while (condition)
{
}

After

do
{
} while (condition);

Replace while statement with for statement (RR0151)

  • Syntax: while statement
  • Span: while keyword or selected statement(s) Replace while statement with for statement

Reverse for loop (RR0152)

  • Syntax: for statement Reverse for loop

Simplify if (RR0153)

  • Syntax: if statement
  • Span: top if keyword or selected if statement Simplify if

Simplify lambda expression (RR0154)

  • Syntax: lambda expression with block with single single-line statement
  • Span: body Simplify lambda expression

Sort case labels (RR0207)

  • Syntax: selected case labels with string literal or enum field

Before

bool Foo(string s)
{
    switch (s)
    {
        case "d":
        case "b":
        case "a":
        case "c":
            return true;
        default:
            return false;
    }
}

After

bool Foo(string s)
{
    switch (s)
    {
        case "a":
        case "b":
        case "c":
        case "d":
            return true;
        default:
            return false;
    }
}

Sort member declarations (RR0155)

  • Syntax: namespace declarations, class declarations, struct declarations, interface declarations, enum declarations
  • Span: selected member declarations Sort member declarations

Sort member declarations

Sort member declarations

Sort member declarations

Split attributes (RR0156)

  • Syntax: selected attribute list Split attributes

Split declaration and initialization (RR0194)

  • Syntax: local variable declaration
  • Span: equals token

Before

var s = GetValue();

After

string s;
s = GetValue();

Split if statement (RR0184)

  • Syntax: if statement that has logical or expression as a condition
  • Span: top if keyword or selected if statement Split if statement

Split if-else (RR0190)

  • Syntax: if statement
  • Span: selected if statement or topmost if keyword

Before

if (condition1)
{
    return Foo1();
{
else if (condition2)
{
    return Foo2();
}
else
{
    return false;
}

After

if (condition1)
{
    return Foo1();
{

if (condition2)
{
    return Foo2();
}

return false;

Split switch labels (RR0157)

  • Syntax: selected switch labels Split switch labels

Split variable declaration (RR0158)

  • Syntax: local declaration, field declaration, event field declaration Split variable declaration

Swap binary operands (RR0159)

  • Syntax: binary expression
  • Span: binary operator

Before

if (x && y)
{
{

After

if (y && x)
{
{

Swap member declarations (RR0161)

  • Syntax: empty line between member declarations Swap member declarations

Uncomment multi-line comment (RR0200)

  • Syntax: multi-line comment

Before

/*string s = null;*/

After

string s = null;

Uncomment single-line comment (RR0163)

  • Syntax: single-line comment(s) Uncomment single-line comment

Use "" instead of string.Empty (RR0168)

  • Syntax: string.Empty field Use "" instead of string.Empty

Use bitwise operation instead of calling 'HasFlag' (RR0164)

  • Syntax: Enum.HasFlag method invocation Use bitwise operation instead of calling 'HasFlag'

Use C# 6.0 dictionary initializer (RR0191)

  • Syntax: collection initializer

Before

var dic = new Dictionary<int, string>() { { 0, "0" } };

After

var dic = new Dictionary<int, string>() { [0] = "0" };

Use coalesce expression instead of if (RR0165)

  • Syntax: if statement
  • Span: top if keyword or selected if statement Use coalesce expression instead of if

Use conditional expression instead of if (RR0166)

  • Syntax: if statement
  • Span: top if keyword or selected if statement Use conditional expression instead of if

Use constant instead of field (RR0128)

  • Syntax: read-only field Use constant instead of field

Use element access instead of 'First/Last'ElementAt' method (RR0167)

  • Syntax: First/Last/ElementAt method invocation
  • Span: method name Use element access instead of 'First/Last'ElementAt' method

Use expression-bodied member (RR0169)

  • Syntax: method, property, indexer, operator
  • Span: body or accessor list Use expression-bodied member

Use lambda expression instead of anonymous method (RR0170)

  • Syntax: anonymous method
  • Span: delegate keyword Use lambda expression instead of anonymous method

Use List<T> instead of yield (RR0183)

  • Syntax: yield return, yield break Use List<T> instead of yield

Use string.Empty instead of "" (RR0171)

  • Syntax: empty string literal Use string.Empty instead of ""

Use StringBuilder instead of concatenation (RR0182)

  • Syntax: string concatenation Use StringBuilder instead of concatenation

Wrap in #if directive (RR0174)

  • Syntax: selected lines Wrap in #if directive

Wrap in condition (RR0172)

  • Syntax: selected statements Wrap in condition

Wrap in else clause (RR0173)

  • Syntax: statement Wrap in else clause

Wrap in region (RR0175)

  • Syntax: selected lines Wrap in region

Wrap in try-catch (RR0176)

  • Syntax: selected statements Wrap in try-catch

Wrap in using statement (RR0177)

  • Syntax: local declaration of type that implements IDisposable Wrap in using statement

(Generated with DotMarkdown)