C#

C#

Made by DeepSource
Consider discarding empty blocks CS-R1012
Bug risk
Minor

Empty blocks do not necessarily add any value to the codebase and should be discarded if possible. Therefore, it is recommended that you either discard this empty block or refactor it accordingly.

Potential memory leak inside for loop due to stackalloc CS-W1025
Bug risk
Critical

A stackalloc expression allocates a block of memory on the stack and is cleaned up only when the controller exits the method. Placing a stackalloc expression inside a loop continuously allocates memory and does not let it get discarded despite the controller exiting the loop's scope. It is therefore recommended that you rethink your approach.

Method marked as TestMethod declared in a non-test class CS-W1013
Bug risk
Major

Test methods are usually denoted via the [TestMethod] attribute. Such methods must be declared in classes with the [TestClass] attribute. Failing to do so might result in the test-suite not executing the said test method. It is therefore recommended that you either drop the [TestMethod] altogether, or add the [TestClass] attribute to the class containing this test.

Audit required: Instance of NotImplementedException CS-A1003
Bug risk
Major

NotImplementedException is usually thrown to indicate that a particular feature or functionality is not yet implemented thus limiting the overall functionality of your application. This check lets you track all such instances across your codebase.

Audit required: Warning disabled explicitly CS-A1006
Bug risk
Major

Preprocessor directives allow you to suppress warnings for a specific section of the code. This is usually done either because the warning is a false positive or that the code itself is flawed. In case this is a valid warning, consider addressing the root cause behind it rather than suppressing it.

Abrupt application exit CS-W1005
Bug risk
Major

Using Environment.Exit(0) terminates the application abruptly in a potentially unsafe manner. An application should always try to quit gracefully irrespective of whether it encounters an error or not. If your application has to terminate, ensure that it implements the required cleanup/dispose methods to at least safely dispose off the resources that it has acquired/locked to the extent possible and then exit cleanly/gracefully.

Consider using L as a suffix for long rather than l CS-R1027
Bug risk
Major
Autofix

Using l (lowercase L) as a suffix to a numeric value indicates that it should be treated as long. However, in some cases, the lowercase L may look similar to the digit 1. It is therefore recommended that you use L as the suffix to avoid confusion.

Deprecated method being invoked CS-W1023
Bug risk
Major

Methods can be marked as obsolete via the [Obsolete] attribute. Doing so produces a warning to the user along with an explanation why it was marked as obsolete. A method may be marked as obsolete for numerous reasons such as performance, security, or due to the fact that a more reliable alternative exists. It is therefore recommended that you do not use such obsolete methods and move to a more modern and reliable alternatives.

Using negative array indices results in IndexOutOfRangeException CS-W1042
Bug risk
Major
Autofix

Languages like Python allow you to access elements from the last by specifying negative indices. However, this does not work in C# and will result in an IndexOutOfRangeException. In order to access elements from the last, consider prefixing the indices with ^ operator.

Audit required: Test is ignored CS-A1005
Bug risk
Major

The [Ignore] attribute is used to ignore a specific test's final result. This is usually done in situations where the test is failing, either due to the fault in the test itself, or that the logic that is being tested is at fault and is not behaving as per requirement. Because this indicates a potential flaw within your application, it is recommended that you fix it as soon as possible.

Exceptions that are created must be used CS-W1043
Bug risk
Major
Autofix

Exceptions are instantiated just like normal classes via the new keyword and then thrown using the throw keyword. However, in this case, the Exception that is being created is neither thrown nor used in any other manner. It is recommended that you either get rid of this statement completely or throw the Exception.

Unreachable code detected CS-W1016
Bug risk
Major
Autofix

Statements placed after the return statement are not executed and is considered as "dead" code. Either adding the return statement was a mistake or that the code placed after the return statement wasn't supposed to be there in the first place. Either way, it is recommended that you rectify this immediately.

Use double.IsNaN() to check if a double is NaN CS-W1003
Bug risk
Critical
Autofix

By specification, NaN is not equal to anything, not even itself. Therefore, comparing any double with NaN will always evaluate to false. The preferred and right approach is to directly use the double's .isNaN method.

Type name contains only lower-cased ASCII characters CS-W1036
Bug risk
Major

Any new keyword added to the C# language will only contain lowercase ASCII characters. It is therefore recommended that your types contain a non-lower ASCII character such as an upper-case character, a numeric, or an underscore. Failing to meet this requirement might cause a clash if C# adds a keyword with the same name in the newer releases, which would block you from migrating to a newer C# version.

Audit required: enum used for permissions is missing the Flags attribute CS-A1004
Bug risk
Critical

Flags attribute is generally used when representing multiple values often via bitwise operators. It is recommended that enums used in implementing permissions use this attribute.

Usage of reserved keywords as variable identifier CS-R1030
Bug risk
Major

Keywords such as async and await have been introduced in C# 5.0 and are called contextual keywords. Contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context. However, it is recommended that you do not use such keywords as identifiers to avoid confusion.

Empty interpolated string CS-W1000
Bug risk
Critical
Autofix

String interpolation requires that you specify the string that is to be interpolated and the required arguments. Failing to specify the required arguments may result in an ill-formatted string. Because it is possible that such strings can critically alter the behaviour and control-flow of the program, it is suggested that you fix it as soon as possible.

Nested switch statements CS-W1001
Bug risk
Minor

Nested switch statements can affect code readability, refactoring and can cause additional bugs to creep in. Therefore, it is recommended that you refactor your code accordingly, i.e., either avoid nested switches or move the inner switch to a separate method/lambda.

Usage of deprecated Threading APIs CS-W1002
Bug risk
Critical

Threading APIs such as CurrentThread.Resume() and CurrentThread.Suspend() are deprecated and no longer maintained. It is therefore recommended that you switch to other classes in System.Threading, such as Monitor, Mutex, Event, or Semaphore, to synchronize Threads or protect resources.

Custom Exceptions should always be public CS-W1004
Bug risk
Major

Exceptions are not limited to a part of the codebase and can be thrown and caught anywhere. It is therefore recommended that your custom Exceptions always be declared as public. Otherwise, you'd have to catch the closest accessible public exception.