Skip to content

Commit

Permalink
Update with new ID and regenerate files
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Apr 27, 2022
1 parent 63fc959 commit d91f724
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ CA1849 | Performance | Disabled | UseAsyncMethodInAsyncContext, [Documentation](
CA1850 | Performance | Info | PreferHashDataOverComputeHashAnalyzer, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1850)
CA1851 | Performance | Disabled | AvoidMultipleEnumerations, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1851)
CA1852 | Performance | Hidden | SealInternalTypes, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
CA1853 | Performance | Info | DoNotGuardDictionaryRemoveByContainsKey, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1853)
CA2019 | Reliability | Info | UseThreadStaticCorrectly, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2019)
CA2259 | Usage | Warning | UseThreadStaticCorrectly, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2259)
CA5404 | Security | Disabled | DoNotDisableTokenValidationChecks, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca5404)
CA5405 | Security | Disabled | DoNotAlwaysSkipTokenValidationInDelegates, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca5405)
CA1852 | Performance | Info | DoNotGuardDictionaryRemoveByContainsKey, [Documentation](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;

namespace Microsoft.NetCore.Analyzers.Performance
Expand Down Expand Up @@ -45,7 +45,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

context.RegisterCodeFix(new DoNotGuardDictionaryRemoveByContainsKeyCodeAction(_ =>
Task.FromResult(ReplaceConditionWithChild(context.Document, root, conditionalSyntax, childStatementSyntax))),
Task.FromResult(ReplaceConditionWithChild(context.Document, root, conditionalSyntax, childStatementSyntax)), MicrosoftNetCoreAnalyzersResources.RemoveRedundantGuardCallCodeFixTitle),
diagnostic);
}

Expand All @@ -55,12 +55,24 @@ protected abstract Document ReplaceConditionWithChild(Document document, SyntaxN
SyntaxNode conditionalOperationNode,
SyntaxNode childOperationNode);

private class DoNotGuardDictionaryRemoveByContainsKeyCodeAction : DocumentChangeAction
private class DoNotGuardDictionaryRemoveByContainsKeyCodeAction : CodeAction

This comment has been minimized.

Copy link
@mavasani

mavasani Apr 28, 2022

Contributor

Do we really need this class? We should instead just use CodeAction.Create API and pass in the required the parameters to the method.

{
public DoNotGuardDictionaryRemoveByContainsKeyCodeAction(Func<CancellationToken, Task<Document>> action)
: base(MicrosoftNetCoreAnalyzersResources.RemoveRedundantGuardCallCodeFixTitle, action,
DoNotGuardDictionaryRemoveByContainsKey.RuleId)
{ }
private readonly Func<CancellationToken, Task<Document>> _createChangedDocument;
public sealed override string Title { get; }

public sealed override string? EquivalenceKey { get; }

public DoNotGuardDictionaryRemoveByContainsKeyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument, string title, string? equivalenceKey = null)
{
_createChangedDocument = createChangedDocument;
Title = title;
EquivalenceKey = equivalenceKey;
}

protected override Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
{
return _createChangedDocument(cancellationToken);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ namespace Microsoft.NetCore.Analyzers.Performance
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class DoNotGuardDictionaryRemoveByContainsKey : DiagnosticAnalyzer
{
internal const string RuleId = "CA1852";

public const string AdditionalDocumentLocationInfoSeparator = ";;";
public static readonly string[] AdditionalDocumentLocationInfoSeparatorArray = new[] { AdditionalDocumentLocationInfoSeparator };
public const string ConditionalOperation = nameof(ConditionalOperation);
public const string ChildStatementOperation = nameof(ChildStatementOperation);
internal const string RuleId = "CA1853";
private const string Remove = nameof(Remove);
private const string ContainsKey = nameof(ContainsKey);

Expand Down
12 changes: 12 additions & 0 deletions src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,18 @@ When a type is not accessible outside its assembly and has no subtypes within it
|CodeFix|True|
---

## [CA1853](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1853): Unnecessary call to 'Dictionary.ContainsKey(key)'

Do not guard 'Dictionary.Remove(key)' with 'Dictionary.ContainsKey(key)'. The former already checks whether the key exists, and will not throw if it does not.

|Item|Value|
|-|-|
|Category|Performance|
|Enabled|True|
|Severity|Info|
|CodeFix|True|
---

## [CA2000](https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000): Dispose objects before losing scope

If a disposable object is not explicitly disposed before all references to it are out of scope, the object will be disposed at some indeterminate time when the garbage collector runs the finalizer of the object. Because an exceptional event might occur that will prevent the finalizer of the object from running, the object should be explicitly disposed instead.
Expand Down
20 changes: 20 additions & 0 deletions src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -2841,6 +2841,26 @@
]
}
},
"CA1853": {
"id": "CA1853",
"shortDescription": "Unnecessary call to 'Dictionary.ContainsKey(key)'",
"fullDescription": "Do not guard 'Dictionary.Remove(key)' with 'Dictionary.ContainsKey(key)'. The former already checks whether the key exists, and will not throw if it does not.",
"defaultLevel": "note",
"helpUri": "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1853",
"properties": {
"category": "Performance",
"isEnabledByDefault": true,
"typeName": "DoNotGuardDictionaryRemoveByContainsKey",
"languages": [
"C#",
"Visual Basic"
],
"tags": [
"Telemetry",
"EnabledRuleInAggressiveMode"
]
}
},
"CA2000": {
"id": "CA2000",
"shortDescription": "Dispose objects before losing scope",
Expand Down
1 change: 1 addition & 0 deletions src/NetAnalyzers/RulesMissingDocumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ CA1311 | <https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-r
CA1420 | <https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1420> | Property, type, or attribute requires runtime marshalling |
CA1421 | <https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1421> | This method uses runtime marshalling even when the 'DisableRuntimeMarshallingAttribute' is applied |
CA1852 | <https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852> | Seal internal types |
CA1853 | <https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1853> | Unnecessary call to 'Dictionary.ContainsKey(key)' |
CA2019 | <https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2019> | Improper 'ThreadStatic' field initialization |
CA2259 | <https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2259> | 'ThreadStatic' only affects static fields |
2 changes: 1 addition & 1 deletion src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Design: CA2210, CA1000-CA1070
Globalization: CA2101, CA1300-CA1311
Mobility: CA1600-CA1601
Performance: HA, CA1800-CA1852
Performance: HA, CA1800-CA1853
Security: CA2100-CA2153, CA2300-CA2330, CA3000-CA3147, CA5300-CA5405
Usage: CA1801, CA1806, CA1816, CA2200-CA2209, CA2211-CA2259
Naming: CA1700-CA1727
Expand Down

0 comments on commit d91f724

Please sign in to comment.