Skip to content

Commit

Permalink
Merge pull request #1375 from SimonCropp/use-pattern-matching-in-casts
Browse files Browse the repository at this point in the history
use pattern matching in casts
  • Loading branch information
baywet authored Oct 2, 2023
2 parents 0f68120 + edc95ed commit 306dda2
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ public static YamlNode Find(this JsonPointer currentPointer, YamlNode baseYamlNo
var pointer = baseYamlNode;
foreach (var token in currentPointer.Tokens)
{
var sequence = pointer as YamlSequenceNode;

if (sequence != null)
if (pointer is YamlSequenceNode sequence)
{
pointer = sequence.Children[Convert.ToInt32(token)];
}
else
{
var map = pointer as YamlMappingNode;
if (map != null)
if (pointer is YamlMappingNode map)
{
if (!map.Children.TryGetValue(new YamlScalarNode(token), out pointer))
{
Expand Down
6 changes: 2 additions & 4 deletions src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
try
{
Context.StartObject(key);
YamlScalarNode scalarNode = n.Value as YamlScalarNode;
if (scalarNode == null)
if (n.Value is not YamlScalarNode scalarNode)
{
throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
}
Expand Down Expand Up @@ -205,8 +204,7 @@ public string GetReferencePointer()

public string GetScalarValue(ValueNode key)
{
var scalarNode = _node.Children[new YamlScalarNode(key.GetScalarValue())] as YamlScalarNode;
if (scalarNode == null)
if (_node.Children[new YamlScalarNode(key.GetScalarValue())] is not YamlScalarNode scalarNode)
{
throw new OpenApiReaderException($"Expected scalar at line {_node.Start.Line} for key {key.GetScalarValue()}", Context);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Microsoft.OpenApi.Readers/YamlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ internal static class YamlHelper
{
public static string GetScalarValue(this YamlNode node)
{
var scalarNode = node as YamlScalarNode;
if (scalarNode == null)
if (node is not YamlScalarNode scalarNode)
{
throw new OpenApiException($"Expected scalar at line {node.Start.Line}");
}
Expand Down
3 changes: 1 addition & 2 deletions src/Microsoft.OpenApi/Validations/OpenApiValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ private void Validate(object item, Type type)
}

// Validate unresolved references as references
var potentialReference = item as IOpenApiReferenceable;
if (potentialReference != null && potentialReference.UnresolvedReference)
if (item is IOpenApiReferenceable potentialReference && potentialReference.UnresolvedReference)
{
type = typeof(IOpenApiReferenceable);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ private static ValidationRuleSet BuildDefaultRuleSet()
foreach (var property in rules)
{
var propertyValue = property.GetValue(null); // static property
ValidationRule rule = propertyValue as ValidationRule;
if (rule != null)
if (propertyValue is ValidationRule rule)
{
ruleSet.Add(rule);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ public static void WriteOptionalObject<T>(
{
if (value != null)
{
var values = value as IEnumerable;
if (values != null && !values.GetEnumerator().MoveNext())
if (value is IEnumerable values && !values.GetEnumerator().MoveNext())
{
return; // Don't render optional empty collections
}
Expand Down
3 changes: 1 addition & 2 deletions test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public static IEnumerable<object[]> GetSchemas()
var json = JObject.Parse(listJsonStr);
foreach (var item in json.Properties())
{
var versions = GetProp(item.Value, "versions") as JObject;
if (versions == null)
if (GetProp(item.Value, "versions") is not JObject versions)
continue;
foreach (var prop in versions.Properties())
{
Expand Down

0 comments on commit 306dda2

Please sign in to comment.