Skip to content

Commit 23d0cdf

Browse files
Fix code analysis suggestions
- Use `TryGetValue()` and `Count`/`Length` to improve performance. - Fix typo in test name. - Sort usings.
1 parent 2a83fcc commit 23d0cdf

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Linq;
67
using System.Reflection;
7-
using System.Collections.Generic;
88

99
namespace Microsoft.OpenApi
1010
{
@@ -150,12 +150,12 @@ public void Add(Type key, List<ValidationRule> rules)
150150
/// <exception cref="OpenApiException">Exception thrown when rule already exists.</exception>
151151
public void Add(Type key, ValidationRule rule)
152152
{
153-
if (!_rulesDictionary.ContainsKey(key))
153+
if (!_rulesDictionary.TryGetValue(key, out var rules))
154154
{
155-
_rulesDictionary[key] = [];
155+
_rulesDictionary[key] = rules = [];
156156
}
157157

158-
if (_rulesDictionary[key].Contains(rule))
158+
if (rules.Contains(rule))
159159
{
160160
throw new OpenApiException(SRResource.Validation_RuleAddTwice);
161161
}
@@ -202,7 +202,7 @@ public void Remove(string ruleName)
202202
}
203203

204204
// Remove types with no rule
205-
_rulesDictionary = _rulesDictionary.Where(r => r.Value.Any()).ToDictionary(r => r.Key, r => r.Value);
205+
_rulesDictionary = _rulesDictionary.Where(r => r.Value.Count > 0).ToDictionary(r => r.Key, r => r.Value);
206206
}
207207

208208
/// <summary>

test/Microsoft.OpenApi.Tests/Validations/ValidationRuleSetTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void RuleSetConstructorsReturnsTheCorrectRules()
5959
}
6060

6161
[Fact]
62-
public void RemoveValidatioRuleGivenTheValidationRuleWorks()
62+
public void RemoveValidationRuleGivenTheValidationRuleWorks()
6363
{
6464
// Arrange
6565
var ruleSet = new ValidationRuleSet(_rulesDictionary);
@@ -162,7 +162,7 @@ public void TryGetValueWorks()
162162
ruleSet.TryGetValue(typeof(OpenApiContact), out var validationRules);
163163

164164
// Assert
165-
Assert.True(validationRules.Any());
165+
Assert.True(validationRules.Count > 0);
166166
Assert.Contains(_contactValidationRule, validationRules);
167167
}
168168

@@ -195,7 +195,7 @@ public void GetValidationRuleTypesReturnsAllSupportedTypes()
195195
var declaredRuleTypeProperties = typeof(ValidationRuleSet).Assembly.GetTypes()
196196
.Where(t => t.IsClass
197197
&& t != typeof(object)
198-
&& t.GetCustomAttributes(typeof(OpenApiRuleAttribute), false).Any())
198+
&& t.GetCustomAttributes(typeof(OpenApiRuleAttribute), false).Length != 0)
199199
.SelectMany(t2 => t2.GetProperties(BindingFlags.Static | BindingFlags.Public))
200200
.ToList();
201201

0 commit comments

Comments
 (0)