220 Rules

Offering a set of powerful rules, SonarC# is all you need to find bugs, vulnerabilities, and code smells in your C# code. With SonarC#, monitoring your code quality is no longer a daunting task.



Code Smell

Bug Detection Rules

Rule ID Name Sonar way Tags In Action
S2275 Composite format strings should not lead to unexpected behavior at runtimecert, pitfall~10 issues
S2178 Short-circuit logic should be used in boolean contextscert~82 issues
S2190 Recursion should not be infinite~17 issues
S2930 "IDisposables" should be disposedcwe, denial-of-service~10 issues
S2931 Classes with "IDisposable" members should implement "IDisposable"cwe, denial-of-service
S3904 Assemblies should have version informationpitfall~242 issues
S1944 Inappropriate casts should not be madecert, cwe, misra, pitfall~88 issues
S3449 Right operands of shift operators should be integers
S2952 Classes should "Dispose" of members from the classes' own "Dispose" methodscwe, denial-of-service
S2551 Types and "this" should not be used for lockingmulti-threading
S3216 "ConfigureAwait(false)" should be usedmulti-threading, suspicious
S1854 Dead stores should be removedcert, cwe, suspicious, unused~2,290 issues
S1698 "==" should not be used when "Equals" is overriddencert, cwe~154 issues
S1764 Identical expressions should not be used on both sides of a binary operatorcert~14 issues
S2225 "ToString()" method should not return nullcwe~2 issues
S2743 Static fields should not be used in generic types~77 issues
S112 General exceptions should never be throwncert, cwe, error-handling~2,103 issues
S1656 Variables should not be self-assignedcert~35 issues
S2995 "Object.ReferenceEquals" should not be used for value types
S2996 "ThreadStatic" fields should not be initializedmulti-threading
S2234 Parameters should be passed in the correct order~33 issues
S1145 Useless "if(true) {...}" and "if(false){...}" blocks should be removedcwe, misra~12 issues
S3445 Exceptions should not be explicitly rethrownconfusing, error-handling~1,210 issues
S3443 Type should not be examined on "System.Type" instancessuspicious
S3168 "async" methods should not return "void"multi-threading~50 issues
S2757 "=+" should not be used instead of "+="~37 issues
S2997 "IDisposables" created in a "using" statement should not be returned
S3603 Methods with "Pure" attribute should return a value
S3655 Empty nullable value should not be accessedcwe~9 issues
S1994 "for" loop incrementers should modify the variable being tested in the loop's stop conditionsuspicious
S2688 "NaN" should not be used in comparisonscert
S2201 Return values should not be ignored when function calls don't have any side effectscert, misra~86 issues
S2681 Multiline blocks should be enclosed in curly bracescert, cwe~142 issues
S3010 Static fields should not be updated in constructors~68 issues
S1244 Floating point numbers should not be tested for equalitymisra~791 issues
S3263 Static fields should appear in the order they must be initialized~2 issues
S1697 Short-circuit logic should be used to prevent null pointer dereferences in conditionals
S3598 One-way "OperationContract" methods should have "void" return type
S3597 "ServiceContract" and "OperationContract" attributes should be used togethersuspicious~2 issues
S3249 Classes directly extending "object" should not call "base" in "GetHashCode" or "Equals"~7 issues
S1862 Related "if/else if" statements should not have the same conditioncert, pitfall, unused~7 issues
S3005 "ThreadStatic" should not be used on non-static fieldsunused
S3244 Anonymous delegates should not be used to unsubscribe from Events
S2437 Silly bit operations should not be performedsuspicious~3 issues
S3610 Nullable type comparison should not be redundantredundant
S3453 Classes should not have only "private" constructorsdesign~50 issues
S2123 Values should not be uselessly incrementedunused~3 issues
S2761 Doubled prefix operators "!!" and "~~" should not be used
S3451 "[DefaultValue]" should not be used when "[DefaultParameterValue]" is meantsuspicious
S3172 Delegates should not be subtracted
S2259 Null pointers should not be dereferencedcert, cwe~154 issues
S3466 Optional parameters should be passed to "base" calls
S1163 Exceptions should not be thrown in finally blockscert, error-handling, suspicious
S2372 Exceptions should not be thrown from property getterserror-handling~384 issues
FailedUn. Failed unit tests should be fixed
S2583 Expressions should not unconditionally evaluate to "true"cert, cwe, misra, pitfall~176 issues
S2184 Results of integer division should not be assigned to floating point variablescert, cwe, misra, overflow, sans-top25-risky~4 issues
S1210 "Equals" and the comparison operators should be overridden when implementing "IComparable"~33 issues
S1206 "Equals(Object)" and "GetHashCode()" should be overridden in pairscert, cwe~4 issues
S2345 Flags enumerations should explicitly initialize all their members~5 issues
S3397 "base.Equals" should not be used to check for reference equality in "Equals" if "base" is not "object"
S2197 Modulus results should not be checked for direct equalitycert, suspicious
S2328 "GetHashCode" should not reference mutable fields~144 issues
S1116 Empty statements should be removedcert, misra, unused~180 issues
S2692 "IndexOf" checks should not be for positive numberssuspicious~20 issues
S2291 Overflow checking should not be disabled for "Enumerable.Sum"error-handling
S3236 Caller information arguments should not be provided explicitlysuspicious~114 issues
S1858 "ToString()" calls should not be redundantclumsy, finding
S2674 The length returned from a stream read should be checkedcert
S2955 Generic parameters not constrained to reference types should not be compared to "null"
S1226 Method parameters and caught exceptions should not be reassignedmisra, pitfall
S3456 "string.ToCharArray()" should not be called redundantlyclumsy~4 issues
S2486 Generic exceptions should not be ignoredcwe, error-handling, suspicious~347 issues
S3220 Method calls should not resolve ambiguously to overloads with "params"pitfall~1,354 issues
S1449 Culture should be specified for "string" operationscert, unpredictable~2,140 issues
S2934 Property assignments should not be made for "readonly" fields not constrained to reference types

Vulnerability Detection Rules

Rule ID Name Sonar way Tags In Action
S2278 Neither DES (Data Encryption Standard) nor DESede (3DES) should be usedcert, cwe, owasp-a6, sans-top25-porous~12 issues
S2070 SHA-1 and Message-Digest hash algorithms should not be usedcwe, owasp-a6, sans-top25-porous
S1313 IP addresses should not be hardcodedcert
S2386 Mutable fields should not be "public static"cert, cwe, unpredictable~61 issues
S1104 Fields should not have public accessibilitycwe~3,067 issues
S2228 Console logging should not be usedowasp-a6

Code Smell Detection Rules

Rule ID Name Sonar way Tags In Action
S3875 "operator==" should not be overloaded on reference typespitfall~15 issues
S3877 Exceptions should not be thrown from unexpected methodspitfall~1 issue
S3884 "CoSetProxyBlanket" and "CoInitializeSecurity" should not be used
S3889 Neither "Thread.Resume" nor "Thread.Suspend" should be usedmulti-threading, unpredictable
S3869 "SafeHandle.DangerousGetHandle" should not be calledleak, unpredictable~1 issue
S3427 Method overloads with default parameter values should not overlappitfall, unused~44 issues
S3237 "value" parameters should be usedpitfall~168 issues
S2387 Child class fields should not shadow parent class fieldsconfusing
S1451 Track lack of copyright and license headers
S2306 "async" and "await" should not be used as identifierspitfall~3 issues
S2953 Methods named "Dispose" should implement "IDisposable.Dispose"pitfall~26 issues
S2368 Public methods should not have multidimensional array parameterspitfall~2 issues
S3776 Cognitive Complexity of methods should not be too highbrain-overload~1,505 issues
S2346 Flags enumerations zero-value members should be named "None"convention~20 issues
S2223 Non-constant static fields should not be visiblepitfall~3,118 issues
S3600 "params" should not be introduced on overridesconfusing
S3447 "[Optional]" should not be used on "ref" or "out" parameterspitfall
S1541 Methods and properties should not be too complexbrain-overload
S121 Control structures should use curly bracescert, misra, pitfall
S2696 Instance members should not write to "static" fieldsmulti-threading~448 issues
S3265 Non-flags enums should not be used in bitwise operationsconvention~244 issues
S2330 Array covariance should not be usedpitfall
S2339 Public constant members should not be usedpitfall
S1006 Method overrides should not change parameter defaultscert, misra, pitfall~486 issues
S2290 Field-like events should not be virtual~2 issues
S1699 Constructors should only call non-overridable methodscert, pitfall~71 issues
S1067 Expressions should not be too complexbrain-overload
S1186 Methods should not be emptysuspicious~860 issues
S3215 "interface" instances should not be cast to concrete typesdesign
S131 "switch/Select" statements should end with "default/Case Else" clausescert, cwe, misra
S134 Control flow statements "if", "switch", "for", "foreach", "while", "do" and "try" should not be nested too deeplybrain-overload~3,735 issues
S3217 "Explicit" conversions of "foreach" loops should not be usedsuspicious~134 issues
S3218 Inner class members should not shadow outer class "static" or type memberscert, design, pitfall~1,225 issues
S927 "partial" method parameter names should matchcert, misra, suspicious
S2360 Optional parameters should not be usedpitfall~5,129 issues
S126 "if ... else if" constructs should end with "else" clausescert, misra
S2357 Fields should be privatepitfall
S1144 Unused private types or members should be removedcert, unused~1,059 issues
S3897 Classes that provide "Equals(<T>)" or override "Equals(Object)" should implement "IEquatable<T>"api-design~39 issues
S3898 Value types should implement "IEquatable<T>"performance
S1121 Assignments should not be made from within sub-expressionscert, cwe, misra, suspicious~987 issues
S1172 Unused method parameters should be removedcert, misra, unused~554 issues
S3926 Deserialization methods should be provided for "OptionalField" membersserialization~1 issue
S3457 Composite format strings should be used correctlycert, confusing~16 issues
S2376 Write-only properties should not be usedpitfall~57 issues
S3903 Types should be defined in named namespaces~2 issues
S3902 "Assembly.GetExecutingAssembly" should not be calledperformance
S110 Inheritance tree of classes should not be too deepdesign~134 issues
S3169 Multiple "OrderBy" calls should not be usedperformance
S1871 Two branches in a conditional structure should not have exactly the same implementationdesign, suspicious~1,594 issues
S3874 "out" and "ref" parameters should not be usedsuspicious
S3871 Exception types should be "public"error-handling~2 issues
S3885 "Assembly.Load" should be usedunpredictable~5 issues
S3880 Finalizers should not be emptyperformance
S3881 "IDisposable" should be implemented correctly~22 issues
S2436 Classes and methods should not have too many generic parametersbrain-overload~33 issues
S3442 "abstract" classes should not have "public" constructorsconfusing~164 issues
S1134 Track uses of "FIXME" tagscwe~117 issues
S907 "goto" statement should not be usedbrain-overload, misra~395 issues
S103 Lines should not be too longconvention
S104 Files should not have too many lines of codebrain-overload~177 issues
S107 Methods should not have too many parametersbrain-overload~1,023 issues
S108 Nested blocks of code should not be left emptysuspicious~797 issues
S122 Statements should be on separate linesconvention
S2758 The ternary operator should not return the same value regardless of the condition~4 issues
S1118 Utility classes should not have public constructorsdesign~562 issues
S1117 Local variables should not shadow class fieldscert, pitfall~289 issues
S2326 Unused type parameters should be removedcert, unused~39 issues
S1479 "switch" statements should not have too many "case" clausesbrain-overload~32 issues
S2971 "IEnumerable" LINQs should be simplifiedclumsy~723 issues
S3264 Events should be invokedunused~7 issues
S3262 "params" should be used on overridesconfusing
S3246 Generic type parameters should be co/contravariant when possibleapi-design~55 issues
S1066 Collapsible "if" statements should be mergedclumsy~1,488 issues
S125 Sections of code should not be "commented out"misra, unused~8,492 issues
S127 "for" loop stop conditions should be invariantmisra, pitfall
S1848 Objects should not be created to be dropped immediately without being used~17 issues
S2933 Fields that are only assigned in the constructor should be "readonly"confusing~8,291 issues
Insuffic. Lines should have sufficient coverage by testsbad-practice
Insuffic. Branches should have sufficient coverage by testsbad-practice
Duplicat. Source files should not have any duplicated blockspitfall
Insuffic. Source files should have a sufficient density of comment linesconvention
SkippedU. Skipped unit tests should be either removed or fixedpitfall
S2342 Enumeration types should comply with a naming conventionconvention~12 issues
S2325 Methods and properties that don't access instance data should be staticpitfall
S3247 Duplicate casts should not be madeperformance~18 issues
S2156 "sealed" classes should not have "protected" membersconfusing
S1155 "Any()" should be used to test for emptinessclumsy~335 issues
S105 Tabulation characters should not be usedconvention
S1643 Strings should not be concatenated using '+' in a loopperformance~319 issues
S3876 Strings or integral types should be used for indexersdesign
S3872 Parameter names should not duplicate the names of their methodsconfusing, convention
S1215 "GC.Collect" should not be calledbad-practice, performance, unpredictable
S3887 Mutable, non-private fields should not be "readonly"~51 issues
S2344 Enumeration type names should not have "Flags" or "Enum" suffixesconvention~43 issues
S1659 Multiple variables should not be declared on the same linecert, convention, misra
S1940 Boolean checks should not be invertedpitfall~481 issues
S1301 "switch" statements should have at least 3 "case" clausesbad-practice, misra
S3444 Interfaces should not simply inherit from base interfaces with colliding membersdesign~2 issues
S3440 Variables should not be checked against the values they're about to be assignedconfusing~11 issues
S3441 Redundant property names should be omitted in anonymous classesclumsy, finding
S3604 Member initializer values should not be redundant~98 issues
S1939 Inheritance list should not be redundantclumsy~131 issues
S3532 Empty "default" clauses should be removedclumsy, finding, unused
S3257 Declarations and initializations should be as concise as possibleclumsy, finding
S3376 Attribute, EventArgs, and Exception type names should end with the type being extendedconvention~65 issues
S3256 "string.IsNullOrEmpty" should be usedclumsy~25 issues
S3253 Constructor and destructor declarations should not be redundantclumsy, finding
S3254 Default parameter values should not be passed as argumentsclumsy, finding
S1905 Redundant casts should not be usedclumsy, redundant~510 issues
S3251 Implementations should be provided for "partial" methodssuspicious~4 issues
S1125 Boolean literals should not be redundantclumsy~1,324 issues
S2333 Redundant modifiers should not be usedclumsy, finding, unused
S101 Types should be named in camel caseconvention~3,283 issues
S100 Methods and properties should be named in camel caseconvention~14,289 issues
S2737 "catch" clauses should do more than rethrowcert, clumsy, finding, unused
S2219 Runtime type checking should be simplifiedclumsy~74 issues
S1481 Unused local variables should be removedunused~1,249 issues
S2292 Trivial properties should be auto-implementedclumsy~2,060 issues
S3261 Namespaces should not be emptycert, unused~22 issues
S3235 Redundant parentheses should not be usedcert, finding, unused
S1694 An abstract class should have both abstract and concrete methodsconvention
S3234 "GC.SuppressFinalize" should not be invoked for types without destructorsconfusing, unused~1 issue
S1450 Private fields only used as local variables in methods should become local variablespitfall~4,936 issues
S1185 Overriding members should do more than simply call the same member in the base classclumsy, redundant~64 issues
S1109 A close curly brace should be located at the beginning of a lineconvention
S1227 break statements should not be used except for switch cases
S3240 The simplest possible condition syntax should be usedclumsy
S3241 Methods should not return values that are never usedcert, design, unused~98 issues
S3458 Empty "case" clauses that fall through to the "default" should be omittedclumsy, finding~50 issues
S2760 Sequential tests should not check the same conditionclumsy, suspicious
S3459 Unassigned members should be removedsuspicious~85 issues
S3450 Parameters with "[DefaultParameterValue]" attributes should also be marked "[Optional]"pitfall
S3052 Members should not be initialized to default valuesconvention, finding
S3626 Jump statements should not be redundantclumsy, finding, redundant~134 issues
S818 Literal suffixes should be upper casecert, convention, misra, pitfall
S1135 Track uses of "TODO" tagscwe~1,853 issues
S1309 Track uses of in-source issue suppressions

Back to the top