├── docs ├── U2U1000.md ├── U2U1001.md ├── U2U1002.md ├── U2U1003.md ├── U2U1004.md ├── U2U1005.md ├── U2U1006.md ├── U2U1007.md ├── U2U1008.md ├── U2U1010.md ├── U2U1011.md ├── U2U1012.md ├── U2U1013.md ├── U2U1014.md ├── U2U1015.md ├── U2U1016.md ├── U2U1017.md ├── U2U1018.md ├── U2U1019.md ├── U2U1020.md ├── U2U1021.md ├── U2U1100.md ├── U2U1101.md ├── U2U1103.md ├── U2U1104.md ├── U2U1105.md ├── U2U1106.md ├── U2U1107.md ├── U2U1108.md ├── U2U1109.md ├── U2U1110.md ├── U2U1111.md ├── U2U1112.md ├── U2U1113.md ├── U2U1114.md ├── U2U1200.md ├── U2U1201.md ├── U2U1202.md ├── U2U1203.md ├── U2U1204.md ├── U2U1205.md ├── U2U1206.md ├── U2U1207.md ├── U2U1208.md ├── U2U1209.md ├── U2U1210.md ├── U2U3000.md ├── U2U3001.md └── U2U1009.md ├── .gitignore └── README.md /docs/U2U1000.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1000.md -------------------------------------------------------------------------------- /docs/U2U1001.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1001.md -------------------------------------------------------------------------------- /docs/U2U1002.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1002.md -------------------------------------------------------------------------------- /docs/U2U1003.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1003.md -------------------------------------------------------------------------------- /docs/U2U1004.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1004.md -------------------------------------------------------------------------------- /docs/U2U1005.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1005.md -------------------------------------------------------------------------------- /docs/U2U1006.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1006.md -------------------------------------------------------------------------------- /docs/U2U1007.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1007.md -------------------------------------------------------------------------------- /docs/U2U1008.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1008.md -------------------------------------------------------------------------------- /docs/U2U1010.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1010.md -------------------------------------------------------------------------------- /docs/U2U1011.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1011.md -------------------------------------------------------------------------------- /docs/U2U1012.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1012.md -------------------------------------------------------------------------------- /docs/U2U1013.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1013.md -------------------------------------------------------------------------------- /docs/U2U1014.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1014.md -------------------------------------------------------------------------------- /docs/U2U1015.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1015.md -------------------------------------------------------------------------------- /docs/U2U1016.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1016.md -------------------------------------------------------------------------------- /docs/U2U1017.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1017.md -------------------------------------------------------------------------------- /docs/U2U1018.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1018.md -------------------------------------------------------------------------------- /docs/U2U1019.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1019.md -------------------------------------------------------------------------------- /docs/U2U1020.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1020.md -------------------------------------------------------------------------------- /docs/U2U1021.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1021.md -------------------------------------------------------------------------------- /docs/U2U1100.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1100.md -------------------------------------------------------------------------------- /docs/U2U1101.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1101.md -------------------------------------------------------------------------------- /docs/U2U1103.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1103.md -------------------------------------------------------------------------------- /docs/U2U1104.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1104.md -------------------------------------------------------------------------------- /docs/U2U1105.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1105.md -------------------------------------------------------------------------------- /docs/U2U1106.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1106.md -------------------------------------------------------------------------------- /docs/U2U1107.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1107.md -------------------------------------------------------------------------------- /docs/U2U1108.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1108.md -------------------------------------------------------------------------------- /docs/U2U1109.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1109.md -------------------------------------------------------------------------------- /docs/U2U1110.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1110.md -------------------------------------------------------------------------------- /docs/U2U1111.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1111.md -------------------------------------------------------------------------------- /docs/U2U1112.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1112.md -------------------------------------------------------------------------------- /docs/U2U1113.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1113.md -------------------------------------------------------------------------------- /docs/U2U1114.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1114.md -------------------------------------------------------------------------------- /docs/U2U1200.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1200.md -------------------------------------------------------------------------------- /docs/U2U1201.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1201.md -------------------------------------------------------------------------------- /docs/U2U1202.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1202.md -------------------------------------------------------------------------------- /docs/U2U1203.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1203.md -------------------------------------------------------------------------------- /docs/U2U1204.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1204.md -------------------------------------------------------------------------------- /docs/U2U1205.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1205.md -------------------------------------------------------------------------------- /docs/U2U1206.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1206.md -------------------------------------------------------------------------------- /docs/U2U1207.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1207.md -------------------------------------------------------------------------------- /docs/U2U1208.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1208.md -------------------------------------------------------------------------------- /docs/U2U1209.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1209.md -------------------------------------------------------------------------------- /docs/U2U1210.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U1210.md -------------------------------------------------------------------------------- /docs/U2U3000.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U3000.md -------------------------------------------------------------------------------- /docs/U2U3001.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/u2uconsult/codeanalyzers/HEAD/docs/U2U3001.md -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio. 3 | ################################################################################ 4 | 5 | /.vs 6 | -------------------------------------------------------------------------------- /docs/U2U1009.md: -------------------------------------------------------------------------------- 1 | # U2U1009: Async or iterator methods should avoid state machine generation for early exits (throws or synchronous returns) 2 | 3 | Category : Performance 4 | 5 | Severity : Warning 6 | 7 | ## Cause 8 | 9 | An `async` method has throws or synchronous returns before the first `await`expression. 10 | 11 | An iterator method has throws before the first `yield return` or `yield break` statement. 12 | 13 | ## Rule Description 14 | 15 | This documentation is under construction. 16 | 17 | ## How to Fix Violations 18 | 19 | Move the part of the method that does require a state machine to be generated into a local function. 20 | 21 | ## Example 22 | 23 | Consider this example: 24 | 25 | ``` 26 | public static IEnumerable Range(int start, int length) 27 | { 28 | if (length < 0) 29 | { 30 | throw new ArgumentOutOfRangeException(nameof(length)); 31 | } 32 | 33 | while (length-- > 0) 34 | { 35 | yield return start++; 36 | } 37 | } 38 | 39 | ``` 40 | 41 | For optimal performance, consider changing that as follows: 42 | 43 | ``` 44 | public static IEnumerable Range(int start, int length) 45 | { 46 | if (length < 0) 47 | { 48 | throw new ArgumentOutOfRangeException(nameof(length)); 49 | } 50 | 51 | return Iterator(); 52 | 53 | IEnumerable Iterator() 54 | { 55 | while (length-- > 0) 56 | { 57 | yield return start++; 58 | } 59 | } 60 | } 61 | ``` 62 | 63 | ## Related Rules 64 | 65 | [U2U1006](U2U1006.md) Await tasks correctly 66 | 67 | 68 | © 2017 U2U Consult 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # U2U Consult Performance Analyzers for C# 2 | 3 | This repository hosts the documentation for the U2U Consult diagnostic code analyzers. 4 | 5 | Download the [U2U Consult Performance Analyzers for C#](https://marketplace.visualstudio.com/items?itemName=vs-publisher-363830.U2UConsultPerformanceCodeAnalyzersforC7) 6 | 7 | Documentation is under construction. You can contribute by raising an issue, or by submitting a pull request. 8 | 9 | | Code | Description | 10 | | -------------------------- | ----------- | 11 | | [U2U1000](docs/U2U1000.md) | Local variable can be inlined or declared const | 12 | | [U2U1001](docs/U2U1001.md) | Stateless classes can be static | 13 | | [U2U1002](docs/U2U1002.md) | Method can be declared static | 14 | | [U2U1003](docs/U2U1003.md) | Avoid declaring methods used in delegate constructors static | 15 | | [U2U1004](docs/U2U1004.md) | Public value types should implement equality | 16 | | [U2U1005](docs/U2U1005.md) | Implement internal interfaces | 17 | | [U2U1006](docs/U2U1006.md) | Await tasks correctly | 18 | | [U2U1007](docs/U2U1007.md) | Do not call redundant functions | 19 | | [U2U1008](docs/U2U1008.md) | Parentheses can be used to enable constant evaluation | 20 | | [U2U1009](docs/U2U1009.md) | Async or iterator methods should avoid state machine generation for early exits (throws or synchronous returns) | 21 | | [U2U1010](docs/U2U1010.md) | Internal leaf classes can be sealed | 22 | | [U2U1011](docs/U2U1011.md) | Return types should be specific | 23 | | [U2U1012](docs/U2U1012.md) | Parameter types should be specific | 24 | | [U2U1013](docs/U2U1013.md) | Return types should not cause boxing | 25 | | [U2U1014](docs/U2U1014.md) | Parameter types should not cause boxing | 26 | | [U2U1015](docs/U2U1015.md) | Do not index an array multiple times within a loop body | 27 | | [U2U1016](docs/U2U1016.md) | Use a CancellationToken when possible | 28 | | [U2U1017](docs/U2U1017.md) | Initialized locals should be used | 29 | | [U2U1018](docs/U2U1018.md) | Local variable types should be specific | 30 | | [U2U1019](docs/U2U1019.md) | Local variable types should not cause boxing | 31 | | [U2U1020](docs/U2U1020.md) | Field types should be specific | 32 | | [U2U1021](docs/U2U1021.md) | Field types should not cause boxing | 33 | | [U2U1100](docs/U2U1100.md) | Compare strings correctly | 34 | | [U2U1101](docs/U2U1101.md) | Do not concatenate values to strings directly | 35 | | [U2U1103](docs/U2U1103.md) | Index strings correctly | 36 | | [U2U1104](docs/U2U1104.md) | Do not use composite formatting to concatenate strings | 37 | | [U2U1105](docs/U2U1105.md) | Do not use string interpolation to concatenate strings | 38 | | [U2U1106](docs/U2U1106.md) | Do not append the result of string concatenation to a StringBuilder | 39 | | [U2U1107](docs/U2U1107.md) | Do not append a single character string to a StringBuilder | 40 | | [U2U1108](docs/U2U1108.md) | StringBuilders should be initialized with capacity | 41 | | [U2U1109](docs/U2U1109.md) | Do not test strings for null when appending to a StringBuilder | 42 | | [U2U1110](docs/U2U1110.md) | Do not append null or empty strings to a StringBuilder | 43 | | [U2U1111](docs/U2U1111.md) | Do not append a substring to a StringBuilder | 44 | | [U2U1112](docs/U2U1112.md) | Do not call string.IsNullOrEmpty() on a constant string | 45 | | [U2U1113](docs/U2U1113.md) | Do not call string.IsNullOrEmpty() on a trimmed string | 46 | | [U2U1114](docs/U2U1114.md) | Do not call string.IsNullOrEmpty() on a normalized string | 47 | | [U2U1200](docs/U2U1200.md) | Prefer generic collections over non-generic ones | 48 | | [U2U1201](docs/U2U1201.md) | Local collections should be initialized with capacity | 49 | | [U2U1202](docs/U2U1202.md) | Use LINQ Count methods efficiently | 50 | | [U2U1203](docs/U2U1203.md) | Use foreach efficiently | 51 | | [U2U1204](docs/U2U1204.md) | Do not implicitly convert an `IQueryable` to an `IEnumerable` | 52 | | [U2U1205](docs/U2U1205.md) | Do not query a LINQ `EntitySet` | 53 | | [U2U1206](docs/U2U1206.md) | Do not use a LINQ where clause before filtering the query | 54 | | [U2U1207](docs/U2U1207.md) | Do not aggregate the result of a LINQ select operation | 55 | | [U2U1208](docs/U2U1208.md) | Do not call LINQ methods whose effect is undone by subsequent methods | 56 | | [U2U1209](docs/U2U1209.md) | Use dictionaries efficiently | 57 | | [U2U1210](docs/U2U1210.md) | Do not materialize an `IEnumerable` unnecessarily | 58 | | [U2U3000](docs/U2U3000.md) | Provide enough arguments to composite formatting | 59 | | [U2U3001](docs/U2U3001.md) | Use the correct format syntax with composite formatting | 60 | 61 | © 2017 U2U Consult 62 | --------------------------------------------------------------------------------