Code Inspections in C#
In C#, ReSharper 2018.2 provides two kinds of code inspections: 746 inspections that detect errors such as broken syntax, unresolved symbols, compiler errors, etc. (you cannot configure or disable any of these inspections), and 558 proprietary code inspections, any of which you can disable or change its severity level.
These code inspections help you detect code issues in design time in all open files, and additionally they allow you to find code issues in specific scope.
Configurable C# inspections are listed below, grouped by their categories.
Code Notification (1 inspection)
This category groups code inspections with minor severity levels.Inspection | |||
---|---|---|---|
| |
Code Style (37 inspections)
Inspections in this category detect violations of code syntax styles. In contrast to most of other code inspections, these inspections can either detect the same code construct as a code issue or not depending on the corresponding code style rule configured on the page of ReSharper options. You can also fix issues that these inspection detect, using code cleanup.Inspection | |||
---|---|---|---|
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
Replace built-in type reference with a CLR type name or a keyword | | | |
| | ||
| | ||
| | ||
| | ||
Use explicit or implicit modifier definition for type members | | | |
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
Use preferred body style (convert to constructor or destructor with preferred body style) | | | |
Use preferred body style (convert to local function with preferred body style) | | | |
Use preferred body style (convert to method or operator with preferred body style) | | | |
Use preferred body style (convert to property, indexer or event with preferred body style) | | | |
Use preferred braces style (enforce braces in 'do-while' statement) | | | |
Use preferred braces style (enforce braces in 'fixed' statement) | | | |
Use preferred braces style (enforce braces in 'for' statement) | | | |
Use preferred braces style (enforce braces in 'foreach' statement) | | | |
Use preferred braces style (enforce braces in 'if' statement) | | | |
Use preferred braces style (enforce braces in 'lock' statement) | | | |
Use preferred braces style (enforce braces in 'using' statement) | | | |
Use preferred braces style (enforce braces in 'while' statement) | | | |
| | ||
| | ||
| | ||
| | ||
| | ||
| |
Common Practices and Code Improvements (138 inspections)
This category groups inspections that hunt for medium severity issues that mainly affect code readability.Inspection | |||
---|---|---|---|
| | ||
Annotation duplicate in hierarchy | | | |
Auto-property can be made get-only (non-private accessibility) | | | |
| | ||
Base member has 'params' parameter, but overrider hasn't | | | |
Check for reference equality instead | | | |
Check for reference equality instead | | | |
Check for reference equality instead | | | |
Check for reference equality instead | | | |
Class can be made sealed(non-inheritable) (non-private accessibility) | | | |
Class can be made sealed(non-inheritable) (private accessibility) | | | |
Container nullability attribute usage with declaration of non-container type | | | |
Convert 'if do while' to 'while | | | |
| | ||
Convert local variable or field to constant (non-private accessibility) | | | |
Convert local variable or field to constant (private accessibility) | | | |
Declaration nullability inferred (parameter is inferred to be not null) | | | |
Declaration nullability inferred (parameter is inferred to be nullable) | | | |
Declaration nullability inferred (type member is inferred to be not null) | | | |
Declaration nullability inferred (type member is inferred to be nullable) | | | |
| | ||
| | ||
| | ||
| | ||
Get-only auto-property is never assigned | | | |
Invert condition | | | |
Iteration variable can be declared with a more specific type | | | |
| | ||
Literal's length can be reduced by using verbatim string | | | |
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
Member can be made static(shared) (non-private accessibility) | | | |
| | ||
Member or type can be made internal(friend) | | | |
Method supports cancellation | | | |
Missing XML comment for private or internal type or member | | | |
| | ||
Nested string interpolation can be inlined | | | |
Nullability attribute usage with declaration of void or value type | | | |
Nullability attribute used with declaration that cannot be directly referenced from other code | | | |
Parameter can be declared with base type | | | |
Parameter type can be IEnumerable<T> (non-private accessibility) | | | |
Parameter type can be IEnumerable<T> (private accessibility) | | | |
Possible mistaken argument | | | |
Possible mistaken call to GetType() | | | |
Possible mistaken call to GetType() | | | |
Remove constructor invocation | | | |
Remove redundant statement | | | |
Remove redundant statement | | | |
Remove ToList() | | | |
Remove ToList() | | | |
Replace with FirstOrDefault($args$) | | | |
Replace with FirstOrDefault($args$) | | | |
Replace with FirstOrDefault($args$) | | | |
Replace with FirstOrDefault($args$) | | | |
Replace with LastOrDefault($args$) | | | |
Replace with LastOrDefault($args$) | | | |
Replace with LastOrDefault($args$) | | | |
Replace with LastOrDefault($args$) | | | |
Replace with OfType<T>() | | | |
Replace with OfType<T>() | | | |
Replace with OfType<T>() | | | |
Replace with OfType<T>().Any() | | | |
Replace with OfType<T>().Any() (replace with OfType<T>().Any(..)) | | | |
Replace with OfType<T>().Count() | | | |
Replace with OfType<T>().Count() (replace with OfType<T>().Count(..)) | | | |
Replace with OfType<T>().First() | | | |
Replace with OfType<T>().First() (replace with OfType<T>().First(..)) | | | |
Replace with OfType<T>().FirstOrDefault() | | | |
Replace with OfType<T>().FirstOrDefault() (replace with OfType<T>().FirstOrDefault(..)) | | | |
Replace with OfType<T>().Last() | | | |
Replace with OfType<T>().Last() (replace with OfType<T>().Last(..)) | | | |
Replace with OfType<T>().LastOrDefault() | | | |
Replace with OfType<T>().LastOrDefault() (replace with OfType<T>().LastOrDefault(..)) | | | |
Replace with OfType<T>().LongCount() | | | |
Replace with OfType<T>().Single() | | | |
Replace with OfType<T>().Single() (replace with OfType<T>().Single(..)) | | | |
Replace with OfType<T>().SingleOrDefault() | | | |
Replace with OfType<T>().SingleOrDefault() (replace with OfType<T>().SingleOrDefault(..)) | | | |
Replace with OfType<T>().Where() (replace with OfType<T>().Where(..)) | | | |
Replace with simple assignment | | | |
Replace with simple assignment | | | |
| | ||
| | ||
Replace with single call to Any(..) | | | |
Replace with single call to Count(..) | | | |
Replace with single call to First(..) | | | |
Replace with single call to FirstOrDefault(..) | | | |
Replace with single call to Last(..) | | | |
Replace with single call to LastOrDefault(..) | | | |
Replace with single call to Single(..) | | | |
Replace with single call to SingleOrDefault(..) | | | |
Replace with SingleOrDefault($args$) | | | |
Replace with SingleOrDefault($args$) | | | |
Replace with SingleOrDefault($args$) | | | |
Replace with SingleOrDefault($args$) | | | |
Return type can be IEnumerable<T> (non-private accessibility) | | | |
| | ||
| | ||
Simplify LINQ expression | | | |
| | ||
| | ||
| | ||
String literal can be inlined | | | |
String.Compare is culture-specific (string.Compare(string, int, string, int) is culture-specific) | | | |
| | ||
| | ||
String.Compare is culture-specific (string.Compare(string, string) is culture-specific) | | | |
String.Compare is culture-specific (string.Compare(string, string, bool) is culture-specific) | | | |
String.Compare is culture-specific (string.Compare(string, string, bool) is culture-specific) | | | |
| | ||
String.EndsWith is culture-specific (string.EndsWith(string) is culture-specific) | | | |
String.IndexOf is culture-specific (string.IndexOf(string) is culture-specific) | | | |
String.IndexOf is culture-specific (string.IndexOf(string, int) is culture-specific) | | | |
String.IndexOf is culture-specific (string.IndexOf(string, int) is culture-specific) | | | |
String.LastIndexOf is culture-specific (string.LastIndexOf(string) is culture-specific) | | | |
String.LastIndexOf is culture-specific (string.LastIndexOf(string, int) is culture-specific) | | | |
String.LastIndexOf is culture-specific (string.LastIndexOf(string, int) is culture-specific) | | | |
String.StartsWith is culture-specific (string.StartsWith(string) is culture-specific) | | | |
try-catch and try-finally statements can be merged | | | |
Type parameter could be declared as covariant or contravariant | | | |
Unreachable code detected | | | |
Use array creation expression | | | |
Use array creation expression | | | |
Use indexed property | | | |
Use 'is' operator | | | |
Use 'is' operator | | | |
| | ||
Use method Any() | | | |
Use method Any() | | | |
Use method Any() | | | |
Use method Any() | | | |
Use method IsInstanceOfType(..) | | | |
| |
Compiler Warnings (61 inspections)
Inspections in this category detect compiler warnings before you compile.Inspection | |||
---|---|---|---|
| | ||
| | ||
| | ||
Alignment value 'value' has a magnitude greater than 'magnitude limit' and may result in a large formatted string. | | | |
| | ||
| | ||
| | ||
Async function without await expression | | | |
| | ||
| | ||
| | ||
| | ||
Cannot resolve reference in XML comment | | | |
Class overrides Object.Equals(object o) but not Object.GetHashCode() | | | |
| | ||
Declaring new protected member in sealed class is the same as declaring it as private | | | |
Default value specified for parameter will have no effect because it applies to a member that is used in contexts that do not allow optional arguments | | | |
Did you mean to use the 'default:' switch label? | | | |
| | ||
| | ||
| | ||
Field is never used | | | |
Filter expression is a constant, consider removing the filter | | | |
| | ||
| | ||
'goto case' value is not implicitly convertible to required type | | | |
| | ||
Introducing a 'Finalize' method can interfere with destructor invocation | | | |
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
Namespace should be default namespace of this project | | | |
Non-accessed field | | | |
Non-accessed local variable | | | |
| | ||
| | ||
| | ||
| | ||
Reference to a volatile field will not be treated as volatile | | | |
| | ||
| | ||
| | ||
The tuple element name is ignored because a different name or no name is specified by the target type. | | | |
The tuple element name is ignored because a different name or no name is specified on the other side of the tuple == or != operator. | | | |
Type parameter has no matching typeparam tag in the XML comment | | | |
Type parameter has the same name as a type parameter from the outer type | | | |
| | ||
Unassigned field | | | |
Unassigned readonly field | | | |
Unused local variable | | | |
| | ||
| | ||
Using 'is' to test compatibility with 'dynamic' is essentially identical to testing compatibility with 'Object' and will succeed for all non-null values | | | |
XML comment has a 'param' tag for 'Parameter', but there is no parameter by that name | | | |
XML comment has a 'typeparam' tag for 'TypeParameter', but there is no type parameter by that name | | | |
XML comment has cref attribute that refers to a type parameter | | | |
| |
Constraints Violations (10 inspections)
This category includes code inspections, mostly with the warning severity level, which detect violations related to symbol attributes, including ReSharper's code annotations, and other similar issues.Inspection | |||
---|---|---|---|
| | ||
BaseTypeRequired attribute supports only classes and interfaces | | | |
Compare with '==' types marked by 'CannotApplyEqualityOperatorAttribute' | | | |
| | ||
Member with '[NotNull]' attribute is not initialized at constructor exit | | | |
| | ||
Possible 'null' assignment to entity with '[NotNull]' attribute | | | |
| | ||
Required base type conflicting another type | | | |
Type specified in '[BaseTypeRequired]' attribute conflicts another type | | |
Formatting (41 inspections)
Inspections in this category detect code code formatting problems.Inspection | |||
---|---|---|---|
| | ||
| | ||
Incorrect blank lines (incorrect number of blank lines near braces) | | | |
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
Incorrect indent (line indent should be restored to the previous level elsewhere) | | | |
Incorrect indent (line indent should not be changed relative to the previous line elsewhere) | | | |
Incorrect indent (line should be indented or outdented elsewhere) | | | |
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
| | ||
Incorrect spacing (around square brackets within a statement) | | | |
| | ||
| | ||
| | ||
| | ||
| |
Language Usage Opportunities (35 inspections)
This category includes code inspections, mostly with the suggestion severity level, which notify you when more advanced language constructs can be used. These inspections detect syntax of outdated language versions and suggest using features from more modern language versions. For most of the supported languages, language version can be detected automatically or set manually.Inspection | |||
---|---|---|---|
Class cannot be instantiated | | | |
Convert 'as' expression type check and the following null check into negated pattern matching | | | |
Convert 'as' expression type check and the following null check into pattern matching | | | |
| | ||
Convert 'if' statement to 'switch' statement | | | |
| | ||
| | ||
| | ||
Convert property to auto-property (when possible) | | | |
Convert property to auto-property with private setter | | | |
Convert static method invocation to extension method call | | | |
| | ||
| | ||
Convert to static class | | | |
| | ||
| | ||
| | ||
'if-return' statement can be re-written as 'return' statement | | | |
Inline 'out' variable declaration | | | |
Introduce optional parameters (non-private accessibility) | | | |
| | ||
| | ||
| | ||
Join null check with assignment (when possible) | | | |
| | ||
| | ||
Merge conditional ?: expression into conditional access (when possible) | | | |
| | ||
Merge sequential checks in && or || expressions (when possible) | | | |
Part of loop's body can be converted into LINQ-expression | | | |
| | ||
Replace if statement with null-propagating code (when possible) | | | |
| | ||
| | ||
| |
NUnit (3 inspections)
These inspections detect code issues related to NUnit tests.Inspection | |||
---|---|---|---|
Make test method method public (nUnit Test methods should have public visibility) | | | |
Remove parameters from test method (nUnit Test methods should not have parameters) | | | |
Remove return value from test method (nUnit Test methods should not return any value) | | |
Potential Code Quality Issues (124 inspections)
This category includes inspections that detect critical issues (code smells), mostly with Error or Warning level. This category also includes inspections that ensure localization assistance.Inspection | |||
---|---|---|---|
| | ||
| | ||
Abstract or virtual(overridable) event is never invoked | | | |
| | ||
| | ||
| | ||
Ambiguous symbols in text argument | | | |
Annotation conflict in hierarchy | | | |
| | ||
Assignment to a property of a readonly field can be useless | | | |
Auto-property accessor is never used (non-private accessibility) | | | |
Auto-property accessor is never used (private accessibility) | | | |
Bitwise operation on enum which is not marked by [Flags] attribute | | | |
Call to base member with implicit default parameters | | | |
Call to 'base.Equals(...)' is reference equality | | | |
Cannot access static symbol in text argument | | | |
Cannot access symbol in text argument | | | |
Cannot resolve property | | | |
Cannot resolve resource | | | |
Cannot resolve resource item | | | |
| | ||
Captured field reference of a marshal-by-reference class may cause a runtime exception | | | |
Captured reference to 'volatile' field will not be treated as 'volatile' | | | |
| | ||
Class is never instantiated (private accessibility) | | | |
| | ||
| | ||
Collection's content is never queried (non-private accessibility) | | | |
Collection's content is never queried (private accessibility) | | | |
| | ||
| | ||
Element is localizable | | | |
| | ||
'Enumerable.Sum' invocation in explicit unchecked context | | | |
| | ||
Event is never subscribed to (non-private accessibility) | | | |
Event is never subscribed to (private accessibility) | | | |
Event never invoked | | | |
Event unsubscription via anonymous delegate | | | |
Exception rethrow possibly intended | | | |
Explicit argument passed to parameter with caller info attribute | | | |
Formatting is specified, but interpolated string expression is not IFormattable | | | |
Function body is too complex to analyze | | | |
Function is recursive on all execution paths | | | |
| | ||
'GC.SuppressFinalize' is invoked for type without destructor | | | |
| | ||
Impure method is called for readonly field of value type | | | |
| | ||
Instance of IEnumerator is never disposed | | | |
Interfaces marked as ServiceContract should declare at least one OperationContract | | | |
Invalid XML documentation comment | | | |
| | ||
| | ||
Iterator never returns | | | |
| | ||
| | ||
| | ||
| | ||
Method is marked as OperationContract but containing type isn't marked as ServiceContract | | | |
Method with optional or 'params' parameter is hidden by overload | | | |
Method with 'params' is invoked while more specific method is available | | | |
Mismatch optional parameter value in overridden method | | | |
Multiple sequential 'OrderBy' invocation | | | |
Non-accessed field (non-private accessibility) | | | |
| | ||
| | ||
'Object.ReferenceEquals' is always false because it is called with value type | | | |
One way operations couldn't return values | | | |
Operator 'is'/'Type Of ... Is ...' can be used | | | |
Overridden GetHashCode calls base 'Object.GetHashCode()' | | | |
| | ||
| | ||
Possible ambiguity while accessing member by interface | | | |
| | ||
| | ||
| | ||
Possible incorrect implementation of Double-Check Locking pattern. Read access to checked field | | | |
| | ||
Possible loss of fraction | | | |
| | ||
Possible 'System.InvalidCastException' | | | |
| | ||
| | ||
| | ||
Possible unassigned object created by 'new' expression | | | |
| | ||
Possibly misleading 'DefaultValueAttribute' usage to define optional parameter value | | | |
Possibly missing comma before indexer initializer | | | |
Possibly unintended linear search in set | | | |
'ref' or 'out' parameter with [Optional] attribute | | | |
Return value of [MustUseReturnValue] annotated method is not used | | | |
Return value of iterator is not used | | | |
| | ||
Right operand of dynamic shift operation should be convertible to 'int' | | | |
| | ||
Similar anonymous type detected nearby | | | |
Similar expressions comparison | | | |
Some values of the enum are not processed inside switch statement | | | |
| | ||
Static member initializer refers to static member below or in other part | | | |
String formatting method problems | | | |
Suspicious type conversion or check | | | |
Tail recursive call may be replaced with loop | | | |
The given expression of 'is' operator is always of the provided type | | | |
The given expression of 'is' operator is never of the provided type | | | |
The source expression always matches the provided pattern | | | |
The source expression is always of pattern's type | | | |
The source expression never matches the provided pattern | | | |
| | ||
Try cast and check for null may be replaced with type check | | | |
Type check and casts can be merged | | | |
Unassigned field (non-private accessibility) | | | |
Unassigned field (private accessibility) | | | |
Unassigned readonly field | | | |
Usage of <inheritdoc /> is invalid. | | | |
Use <inheritdoc /> on root level to inherit documentation from base candidate. | | | |
| | ||
Use format specifier in format strings | | | |
Use format specifier in interpolated strings | | | |
| | ||
| | ||
'void' method is annotated by [MustUseReturnValue] attribute | | | |
'void' method is annotated by [Pure] attribute | | |
Redundancies in Code (63 inspections)
Code inspections in this category look for redundancies and dead code, which affect code readability and style, and could be safely removed. Some code redundancies cannot be fixed automatically, and quick-fixes for them are performed in the interactive mode, requiring the user input. But the majority of the redundancies can be fixed without user interaction, using either fix in scope or code cleanup.Inspection | |||
---|---|---|---|
| | ||
Anonymous method signature is not necessary | | | |
Array creation can be replaced with array initializer | | | |
| | ||
Assignment results are fully discarded | | | |
Conditional access qualifier expression is known to be null or not null | | | |
| | ||
| | ||
| | ||
Expression is always 'null' | | | |
| | ||
Heuristically unreachable code | | | |
Immediate delegate invocation | | | |
| | ||
| | ||
Redundant [AttributeUsage] attribute property assignment | | | |
Redundant anonymous type property explicit name | | | |
| | ||
Redundant arithmetic overflow checking context | | | |
Redundant 'base.' qualifier | | | |
Redundant boolean comparison | | | |
Redundant braces in collection initializer | | | |
| | ||
| | ||
Redundant cast to 'object' | | | |
| | ||
Redundant comma in array/object/collection/anonymous type initializer | | | |
Redundant comma in attribute list | | | |
Redundant comma in enum declaration | | | |
Redundant condition check before assignments | | | |
Redundant conditional ternary expression usage | | | |
| | ||
| | ||
Redundant empty argument list on object creation expression | | | |
Redundant empty finally block | | | |
Redundant empty object or collection initializer | | | |
| | ||
Redundant explicit array creation in argument of 'params' parameter | | | |
Redundant explicit nullable type creation | | | |
| | ||
Redundant explicit tuple component name | | | |
| | ||
| | ||
Redundant 'IEnumerable.Cast<T>' or 'IEnumerable.OfType<T>' call | | | |
| | ||
| | ||
| | ||
Redundant 'object.ToString()' call | | | |
Redundant 'object.ToString()' call for value types | | | |
| | ||
Redundant 'orderby' clause 'ascending' keyword | | | |
Redundant semicolon after type or namespace declaration | | | |
Redundant string interpolation | | | |
| | ||
Redundant 'string.ToCharArray()' call | | | |
Redundant type arguments of method | | | |
| | ||
| | ||
Redundant verbatim prefix | | | |
Redundant verbatim string prefix | | | |
'true' is redundant as 'for'-statement condition | | | |
Type check for nullable type is equal to underlying type check | | | |
Unsafe context declaration is redundant | | |
Redundancies in Symbol Declarations (42 inspections)
This category includes code inspections, mostly with the warning severity level, which detect empty and unused symbol declarations.Inspection | |||
---|---|---|---|
Class with virtual(overridable) members never inherited (non-private accessibility) | | | |
Class with virtual(overridable) members never inherited (private accessibility) | | | |
Component of the tuple is never used | | | |
'DefaultParameterValueAttribute' should be used in conjunction with 'OptionalAttribute' | | | |
| | ||
| | ||
| | ||
Local entity is only used to capture its name | | | |
| | ||
Local function is never used | | | |
Local function return value is never used | | | |
Method return value is never used (non-private accessibility) | | | |
Method return value is never used (private accessibility) | | | |
Non-accessed local variable | | | |
Parameter is only used for precondition check (non-private accessibility) | | | |
Parameter is only used for precondition check (private accessibility) | | | |
'params' modifier is always ignored on overrides | | | |
Redundant base constructor call | | | |
Redundant class or interface specification in base types list | | | |
| | ||
| | ||
Redundant method overload (non-private accessibility) | | | |
Redundant method overload (private accessibility) | | | |
| | ||
| | ||
| | ||
Type member is never accessed via base type (non-private accessibility) | | | |
Type member is never accessed via base type (private accessibility) | | | |
Type member is only used in overrides (non-private accessibility) | | | |
Type member is only used in overrides (private accessibility) | | | |
| | ||
| | ||
| | ||
Unused label | | | |
| | ||
Unused parameter (non-private accessibility) | | | |
Unused parameter (private accessibility) | | | |
| | ||
Unused parameter of local function | | | |
Unused type parameter | | | |
Virtual(overridable) member is never overridden (non-private accessibility) | | | |
Virtual(overridable) member is never overridden (private accessibility) | | |
Spelling issues (3 inspections)
These inspections detect typos in various contexts.Inspection | |||
---|---|---|---|
| | ||
| | ||
| |