Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ If you are already using other analyzers, you can check [which rules are duplica
|[MA0020](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0020.md)|Performance|Use direct methods instead of LINQ methods|ℹ️|✔️|✔️|✔️|
|[MA0021](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0021.md)|Usage|Use StringComparer.GetHashCode instead of string.GetHashCode|⚠️|✔️|✔️|❌|
|[MA0022](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0022.md)|Design|Return Task.FromResult instead of returning null|⚠️|✔️|✔️|❌|
|[MA0023](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0023.md)|Performance|Add RegexOptions.ExplicitCapture|⚠️|✔️|✔️|❌|
|[MA0023](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0023.md)|Performance|Use RegexOptions.ExplicitCapture or named groups|⚠️|✔️|✔️|❌|
|[MA0024](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0024.md)|Usage|Use an explicit StringComparer when possible|⚠️|✔️|✔️|❌|
|[MA0025](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0025.md)|Design|Implement the functionality instead of throwing NotImplementedException|⚠️|✔️|❌|❌|
|[MA0026](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0026.md)|Design|Fix TODO comment|⚠️|✔️|❌|❌|
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
|[MA0020](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0020.md)|Performance|Use direct methods instead of LINQ methods|<span title='Info'>ℹ️</span>|✔️|✔️|<span title='MA0020.report_when_conversion_needed'>✔️</span>|
|[MA0021](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0021.md)|Usage|Use StringComparer.GetHashCode instead of string.GetHashCode|<span title='Warning'>⚠️</span>|✔️|✔️|❌|
|[MA0022](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0022.md)|Design|Return Task.FromResult instead of returning null|<span title='Warning'>⚠️</span>|✔️|✔️|❌|
|[MA0023](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0023.md)|Performance|Add RegexOptions.ExplicitCapture|<span title='Warning'>⚠️</span>|✔️|✔️|❌|
|[MA0023](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0023.md)|Performance|Use RegexOptions.ExplicitCapture or named groups|<span title='Warning'>⚠️</span>|✔️|✔️|❌|
|[MA0024](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0024.md)|Usage|Use an explicit StringComparer when possible|<span title='Warning'>⚠️</span>|✔️|✔️|❌|
|[MA0025](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0025.md)|Design|Implement the functionality instead of throwing NotImplementedException|<span title='Warning'>⚠️</span>|✔️|❌|❌|
|[MA0026](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0026.md)|Design|Fix TODO comment|<span title='Warning'>⚠️</span>|✔️|❌|❌|
Expand Down
7 changes: 5 additions & 2 deletions docs/Rules/MA0023.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# MA0023 - Add RegexOptions.ExplicitCapture
# MA0023 - Use RegexOptions.ExplicitCapture or named groups
<!-- sources -->
Sources: [GeneratedRegexAttributeUsageAnalyzer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer/Rules/GeneratedRegexAttributeUsageAnalyzer.cs), [RegexMethodUsageAnalyzer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer/Rules/RegexMethodUsageAnalyzer.cs), [UseRegexExplicitCaptureOptionsFixer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer.CodeFixers/Rules/UseRegexExplicitCaptureOptionsFixer.cs)
<!-- sources -->

Using named groups clarifies what is to be captured. It also makes the regex more performant, as unnamed groups will not be captured needlessly.
Use named groups for groups that should be captured, or add `RegexOptions.ExplicitCapture` to prevent unnamed groups from being captured needlessly.

````c#
new Regex("a(b)"); // non-compliant
Expand All @@ -15,4 +15,7 @@ private static partial Regex SampleRegex { get; }

[GeneratedRegex("a(b)", RegexOptions.ExplicitCapture, matchTimeoutMilliseconds: 1000)] // ok
private static partial Regex SampleRegex { get; }

[GeneratedRegex("a(?<name>b)", RegexOptions.None, matchTimeoutMilliseconds: 1000)] // ok
private static partial Regex SampleRegex { get; }
````
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dotnet_diagnostic.MA0021.severity = error
# MA0022: Return Task.FromResult instead of returning null
dotnet_diagnostic.MA0022.severity = error

# MA0023: Add RegexOptions.ExplicitCapture
# MA0023: Use RegexOptions.ExplicitCapture or named groups
dotnet_diagnostic.MA0023.severity = error

# MA0024: Use an explicit StringComparer when possible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dotnet_diagnostic.MA0021.severity = suggestion
# MA0022: Return Task.FromResult instead of returning null
dotnet_diagnostic.MA0022.severity = suggestion

# MA0023: Add RegexOptions.ExplicitCapture
# MA0023: Use RegexOptions.ExplicitCapture or named groups
dotnet_diagnostic.MA0023.severity = suggestion

# MA0024: Use an explicit StringComparer when possible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dotnet_diagnostic.MA0021.severity = warning
# MA0022: Return Task.FromResult instead of returning null
dotnet_diagnostic.MA0022.severity = warning

# MA0023: Add RegexOptions.ExplicitCapture
# MA0023: Use RegexOptions.ExplicitCapture or named groups
dotnet_diagnostic.MA0023.severity = warning

# MA0024: Use an explicit StringComparer when possible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dotnet_diagnostic.MA0021.severity = warning
# MA0022: Return Task.FromResult instead of returning null
dotnet_diagnostic.MA0022.severity = warning

# MA0023: Add RegexOptions.ExplicitCapture
# MA0023: Use RegexOptions.ExplicitCapture or named groups
dotnet_diagnostic.MA0023.severity = warning

# MA0024: Use an explicit StringComparer when possible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dotnet_diagnostic.MA0021.severity = none
# MA0022: Return Task.FromResult instead of returning null
dotnet_diagnostic.MA0022.severity = none

# MA0023: Add RegexOptions.ExplicitCapture
# MA0023: Use RegexOptions.ExplicitCapture or named groups
dotnet_diagnostic.MA0023.severity = none

# MA0024: Use an explicit StringComparer when possible
Expand Down
6 changes: 3 additions & 3 deletions src/Meziantou.Analyzer/Rules/RegexUsageAnalyzerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public abstract class RegexUsageAnalyzerBase : DiagnosticAnalyzer

private static readonly DiagnosticDescriptor ExplicitCaptureRule = new(
RuleIdentifiers.UseRegexExplicitCaptureOptions,
title: "Add RegexOptions.ExplicitCapture",
messageFormat: "Add RegexOptions.ExplicitCapture to prevent capturing unneeded groups",
title: "Use RegexOptions.ExplicitCapture or named groups",
messageFormat: "Use RegexOptions.ExplicitCapture or named groups to prevent capturing unneeded groups",
RuleCategories.Performance,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
Expand Down Expand Up @@ -203,4 +203,4 @@ static bool HasUnnamedGroups(string pattern, RegexOptions options)
}
}
}
}
}