提交 42914d93 编写于 作者: G Gert Driesen 提交者: James Newton-King

Declare out variables inline, and use discards (#873)

上级 3c9086e3
......@@ -125,15 +125,13 @@ namespace Microsoft.AspNetCore.Routing.DecisionTree
unsatisfiedCriteria++;
Criterion criterion;
if (!criteria.TryGetValue(kvp.Key, out criterion))
if (!criteria.TryGetValue(kvp.Key, out var criterion))
{
criterion = new Criterion(comparer);
criteria.Add(kvp.Key, criterion);
}
List<ItemDescriptor<TItem>> branch;
if (!criterion.TryGetValue(kvp.Value, out branch))
if (!criterion.TryGetValue(kvp.Value, out var branch))
{
branch = new List<ItemDescriptor<TItem>>();
criterion.Add(kvp.Value, branch);
......
......@@ -30,17 +30,15 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is bool)
{
return true;
}
bool result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return bool.TryParse(valueString, out result);
return bool.TryParse(valueString, out _);
}
return false;
......
......@@ -36,17 +36,15 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is DateTime)
{
return true;
}
DateTime result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return DateTime.TryParse(valueString, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
return DateTime.TryParse(valueString, CultureInfo.InvariantCulture, DateTimeStyles.None, out _);
}
return false;
......
......@@ -30,17 +30,15 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is decimal)
{
return true;
}
decimal result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return decimal.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out result);
return decimal.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out _);
}
return false;
......
......@@ -30,21 +30,19 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is double)
{
return true;
}
double result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return double.TryParse(
valueString,
NumberStyles.Float | NumberStyles.AllowThousands,
CultureInfo.InvariantCulture,
out result);
out _);
}
return false;
......
......@@ -30,21 +30,19 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is float)
{
return true;
}
float result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return float.TryParse(
valueString,
NumberStyles.Float | NumberStyles.AllowThousands,
CultureInfo.InvariantCulture,
out result);
out _);
}
return false;
......
......@@ -32,17 +32,15 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is Guid)
{
return true;
}
Guid result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return Guid.TryParse(valueString, out result);
return Guid.TryParse(valueString, out _);
}
return false;
......
......@@ -76,8 +76,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
// signal that he is generating a URI that will be used for an HTTP POST, so he wants the URI
// generation to be performed by the (b) route instead of the (a) route, consistent with what would
// happen on incoming requests.
object obj;
if (!values.TryGetValue(routeKey, out obj))
if (!values.TryGetValue(routeKey, out var obj))
{
return true;
}
......
......@@ -30,17 +30,15 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is int)
{
return true;
}
int result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return int.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
return int.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out _);
}
return false;
......
......@@ -87,8 +87,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
var length = valueString.Length;
......
......@@ -30,17 +30,15 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
if (value is long)
{
return true;
}
long result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
return long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out _);
}
return false;
......
......@@ -50,8 +50,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return valueString.Length <= MaxLength;
......
......@@ -44,12 +44,10 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
long longValue;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
if (long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out longValue))
if (long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
{
return longValue <= Max;
}
......
......@@ -50,8 +50,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return valueString.Length >= MinLength;
......
......@@ -44,12 +44,10 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
long longValue;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
if (long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out longValue))
if (long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
{
return longValue >= Min;
}
......
......@@ -40,8 +40,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value))
if (values.TryGetValue(routeKey, out var value))
{
return InnerConstraint.Match(httpContext,
route,
......
......@@ -58,12 +58,10 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
long longValue;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
if (Int64.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out longValue))
if (long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
{
return longValue >= Min && longValue <= Max;
}
......
......@@ -54,9 +54,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object routeValue;
if (values.TryGetValue(routeKey, out routeValue)
if (values.TryGetValue(routeKey, out var routeValue)
&& routeValue != null)
{
var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture);
......
......@@ -34,8 +34,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
if (values.TryGetValue(routeKey, out var value) && value != null)
{
// In routing the empty string is equivalent to null, which is equivalent to an unset value.
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
......
......@@ -41,9 +41,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
throw new ArgumentNullException(nameof(values));
}
object routeValue;
if (values.TryGetValue(routeKey, out routeValue)
if (values.TryGetValue(routeKey, out var routeValue)
&& routeValue != null)
{
var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture);
......
......@@ -87,11 +87,9 @@ namespace Microsoft.AspNetCore.Routing.Internal
var criterion = node.Criteria[i];
var key = criterion.Key;
object value;
if (values.TryGetValue(key, out value))
if (values.TryGetValue(key, out var value))
{
DecisionTreeNode<OutboundMatch> branch;
if (criterion.Branches.TryGetValue(value ?? string.Empty, out branch))
if (criterion.Branches.TryGetValue(value ?? string.Empty, out var branch))
{
Walk(results, values, ambientValues, branch, isFallbackPath);
}
......
......@@ -91,8 +91,8 @@ namespace Microsoft.AspNetCore.Routing
if (!string.IsNullOrEmpty(context.RouteName))
{
VirtualPathData namedRoutePathData = null;
INamedRouter matchedNamedRoute;
if (_namedRoutes.TryGetValue(context.RouteName, out matchedNamedRoute))
if (_namedRoutes.TryGetValue(context.RouteName, out var matchedNamedRoute))
{
namedRoutePathData = matchedNamedRoute.GetVirtualPath(context);
}
......
......@@ -184,8 +184,7 @@ namespace Microsoft.AspNetCore.Routing
private void Add(string key, IRouteConstraint constraint)
{
List<IRouteConstraint> list;
if (!_constraints.TryGetValue(key, out list))
if (!_constraints.TryGetValue(key, out var list))
{
list = new List<IRouteConstraint>();
_constraints.Add(key, list);
......
......@@ -51,8 +51,7 @@ namespace Microsoft.AspNetCore.Routing
{
if (routeDirection.Equals(RouteDirection.IncomingRequest))
{
object routeValue;
routeValues.TryGetValue(kvp.Key, out routeValue);
routeValues.TryGetValue(kvp.Key, out var routeValue);
logger.RouteValueDoesNotMatchConstraint(routeValue, kvp.Key, kvp.Value);
}
......
......@@ -49,8 +49,7 @@ namespace Microsoft.AspNetCore.Routing.Template
continue;
}
object value;
if (Defaults.TryGetValue(part.Name, out value))
if (Defaults.TryGetValue(part.Name, out var value))
{
_hasDefaultValue[i] = true;
_defaultValues[i] = value;
......
......@@ -91,9 +91,8 @@ namespace Microsoft.AspNetCore.Routing.Tree
if (next.Literals.Count > 0)
{
UrlMatchingNode node;
Debug.Assert(next.Depth < _tokenizer.Count);
if (next.Literals.TryGetValue(_tokenizer[next.Depth].Value, out node))
if (next.Literals.TryGetValue(_tokenizer[next.Depth].Value, out var node))
{
_stack.Push(node);
}
......
......@@ -259,8 +259,7 @@ namespace Microsoft.AspNetCore.Routing.Tree
foreach (var entry in InboundEntries)
{
UrlMatchingTree tree;
if (!trees.TryGetValue(entry.Order, out tree))
if (!trees.TryGetValue(entry.Order, out var tree))
{
tree = new UrlMatchingTree(entry.Order);
trees.Add(entry.Order, tree);
......
......@@ -103,8 +103,7 @@ namespace Microsoft.AspNetCore.Routing.Tree
// We only need to keep one OutboundMatch per route template
// so in case two entries have the same name and the same template we only keep
// the first entry.
OutboundMatch namedMatch;
if (_namedEntries.TryGetValue(entry.RouteName, out namedMatch) &&
if (_namedEntries.TryGetValue(entry.RouteName, out var namedMatch) &&
!string.Equals(
namedMatch.Entry.RouteTemplate.TemplateText,
entry.RouteTemplate.TemplateText,
......@@ -233,8 +232,7 @@ namespace Microsoft.AspNetCore.Routing.Tree
private VirtualPathData GetVirtualPathForNamedRoute(VirtualPathContext context)
{
OutboundMatch match;
if (_namedEntries.TryGetValue(context.RouteName, out match))
if (_namedEntries.TryGetValue(context.RouteName, out var match))
{
var path = GenerateVirtualPath(context, match.Entry, match.TemplateBinder);
if (path != null)
......
......@@ -86,8 +86,7 @@ namespace Microsoft.AspNetCore.Routing.Tree
var part = segment.Parts[0];
if (part.IsLiteral)
{
UrlMatchingNode next;
if (!current.Literals.TryGetValue(part.Text, out next))
if (!current.Literals.TryGetValue(part.Text, out var next))
{
next = new UrlMatchingNode(length: i + 1);
current.Literals.Add(part.Text, next);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册