From c18fc3d7ca468628c6bbb7030014ab94f15f83cf Mon Sep 17 00:00:00 2001 From: YPTopMan Date: Fri, 7 Jun 2019 00:56:12 +0800 Subject: [PATCH] Use throws expressions and expression bodied members (#10764) --- src/Mvc/Mvc.Core/src/ControllerBase.cs | 16 ++++---------- src/Mvc/Mvc.Core/src/PhysicalFileResult.cs | 17 ++------------ src/Mvc/Mvc.Core/src/ProducesAttribute.cs | 9 ++------ .../src/ProducesResponseTypeAttribute.cs | 7 +----- src/Mvc/Mvc.Core/src/RouteAttribute.cs | 7 +----- .../Mvc.Core/src/ServiceFilterAttribute.cs | 7 +----- src/Mvc/Mvc.Core/src/SignInResult.cs | 16 +++----------- src/Mvc/Mvc.Core/src/SignOutResult.cs | 9 ++------ src/Mvc/Mvc.Core/src/TypeFilterAttribute.cs | 7 +----- src/Mvc/Mvc.Core/src/VirtualFileResult.cs | 22 +++---------------- .../PropertyActivator/PropertyActivator.cs | 18 ++++----------- src/Shared/PropertyHelper/PropertyHelper.cs | 9 ++------ 12 files changed, 26 insertions(+), 118 deletions(-) diff --git a/src/Mvc/Mvc.Core/src/ControllerBase.cs b/src/Mvc/Mvc.Core/src/ControllerBase.cs index 359b43c14d..1bca7a09f3 100644 --- a/src/Mvc/Mvc.Core/src/ControllerBase.cs +++ b/src/Mvc/Mvc.Core/src/ControllerBase.cs @@ -212,12 +212,10 @@ namespace Microsoft.AspNetCore.Mvc [NonAction] public virtual ObjectResult StatusCode([ActionResultStatusCode] int statusCode, [ActionResultObjectValue] object value) { - var result = new ObjectResult(value) + return new ObjectResult(value) { StatusCode = statusCode }; - - return result; } /// @@ -270,13 +268,11 @@ namespace Microsoft.AspNetCore.Mvc [NonAction] public virtual ContentResult Content(string content, MediaTypeHeaderValue contentType) { - var result = new ContentResult + return new ContentResult { Content = content, ContentType = contentType?.ToString() }; - - return result; } /// @@ -1772,9 +1768,7 @@ namespace Microsoft.AspNetCore.Mvc /// The created for the response. [NonAction] public virtual UnprocessableEntityResult UnprocessableEntity() - { - return new UnprocessableEntityResult(); - } + => new UnprocessableEntityResult(); /// /// Creates an that produces a response. @@ -1783,9 +1777,7 @@ namespace Microsoft.AspNetCore.Mvc /// The created for the response. [NonAction] public virtual UnprocessableEntityObjectResult UnprocessableEntity([ActionResultObjectValue] object error) - { - return new UnprocessableEntityObjectResult(error); - } + => new UnprocessableEntityObjectResult(error); /// /// Creates an that produces a response. diff --git a/src/Mvc/Mvc.Core/src/PhysicalFileResult.cs b/src/Mvc/Mvc.Core/src/PhysicalFileResult.cs index 27a02b4d94..c3c8e5f35b 100644 --- a/src/Mvc/Mvc.Core/src/PhysicalFileResult.cs +++ b/src/Mvc/Mvc.Core/src/PhysicalFileResult.cs @@ -41,12 +41,7 @@ namespace Microsoft.AspNetCore.Mvc public PhysicalFileResult(string fileName, MediaTypeHeaderValue contentType) : base(contentType?.ToString()) { - if (fileName == null) - { - throw new ArgumentNullException(nameof(fileName)); - } - - FileName = fileName; + FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); } /// @@ -55,15 +50,7 @@ namespace Microsoft.AspNetCore.Mvc public string FileName { get => _fileName; - set - { - if (value == null) - { - throw new ArgumentNullException(nameof(value)); - } - - _fileName = value; - } + set => _fileName = value ?? throw new ArgumentNullException(nameof(value)); } /// diff --git a/src/Mvc/Mvc.Core/src/ProducesAttribute.cs b/src/Mvc/Mvc.Core/src/ProducesAttribute.cs index 41d1815bd9..24218189e5 100644 --- a/src/Mvc/Mvc.Core/src/ProducesAttribute.cs +++ b/src/Mvc/Mvc.Core/src/ProducesAttribute.cs @@ -25,13 +25,8 @@ namespace Microsoft.AspNetCore.Mvc /// /// The of object that is going to be written in the response. public ProducesAttribute(Type type) - { - if (type == null) - { - throw new ArgumentNullException(nameof(type)); - } - - Type = type; + { + Type = type ?? throw new ArgumentNullException(nameof(type)); ContentTypes = new MediaTypeCollection(); } diff --git a/src/Mvc/Mvc.Core/src/ProducesResponseTypeAttribute.cs b/src/Mvc/Mvc.Core/src/ProducesResponseTypeAttribute.cs index 7c0d25c6a4..2af82b4ef6 100644 --- a/src/Mvc/Mvc.Core/src/ProducesResponseTypeAttribute.cs +++ b/src/Mvc/Mvc.Core/src/ProducesResponseTypeAttribute.cs @@ -29,12 +29,7 @@ namespace Microsoft.AspNetCore.Mvc /// The HTTP response status code. public ProducesResponseTypeAttribute(Type type, int statusCode) { - if (type == null) - { - throw new ArgumentNullException(nameof(type)); - } - - Type = type; + Type = type ?? throw new ArgumentNullException(nameof(type)); StatusCode = statusCode; } diff --git a/src/Mvc/Mvc.Core/src/RouteAttribute.cs b/src/Mvc/Mvc.Core/src/RouteAttribute.cs index db42bd2bc1..cc17d61799 100644 --- a/src/Mvc/Mvc.Core/src/RouteAttribute.cs +++ b/src/Mvc/Mvc.Core/src/RouteAttribute.cs @@ -20,12 +20,7 @@ namespace Microsoft.AspNetCore.Mvc /// The route template. May not be null. public RouteAttribute(string template) { - if (template == null) - { - throw new ArgumentNullException(nameof(template)); - } - - Template = template; + Template = template ?? throw new ArgumentNullException(nameof(template)); } /// diff --git a/src/Mvc/Mvc.Core/src/ServiceFilterAttribute.cs b/src/Mvc/Mvc.Core/src/ServiceFilterAttribute.cs index 9e16cb4715..e9d69ff292 100644 --- a/src/Mvc/Mvc.Core/src/ServiceFilterAttribute.cs +++ b/src/Mvc/Mvc.Core/src/ServiceFilterAttribute.cs @@ -30,12 +30,7 @@ namespace Microsoft.AspNetCore.Mvc /// The of filter to find. public ServiceFilterAttribute(Type type) { - if (type == null) - { - throw new ArgumentNullException(nameof(type)); - } - - ServiceType = type; + ServiceType = type ?? throw new ArgumentNullException(nameof(type)); } /// diff --git a/src/Mvc/Mvc.Core/src/SignInResult.cs b/src/Mvc/Mvc.Core/src/SignInResult.cs index d7bdfd3867..0f0ba36f20 100644 --- a/src/Mvc/Mvc.Core/src/SignInResult.cs +++ b/src/Mvc/Mvc.Core/src/SignInResult.cs @@ -35,19 +35,9 @@ namespace Microsoft.AspNetCore.Mvc /// The claims principal containing the user claims. /// used to perform the sign-in operation. public SignInResult(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties) - { - if (authenticationScheme == null) - { - throw new ArgumentNullException(nameof(authenticationScheme)); - } - - if (principal == null) - { - throw new ArgumentNullException(nameof(principal)); - } - - AuthenticationScheme = authenticationScheme; - Principal = principal; + { + AuthenticationScheme = authenticationScheme ?? throw new ArgumentNullException(nameof(authenticationScheme)); + Principal = principal ?? throw new ArgumentNullException(nameof(principal)); Properties = properties; } diff --git a/src/Mvc/Mvc.Core/src/SignOutResult.cs b/src/Mvc/Mvc.Core/src/SignOutResult.cs index 0f69354784..61a9331bab 100644 --- a/src/Mvc/Mvc.Core/src/SignOutResult.cs +++ b/src/Mvc/Mvc.Core/src/SignOutResult.cs @@ -62,13 +62,8 @@ namespace Microsoft.AspNetCore.Mvc /// The authentication scheme to use when signing out the user. /// used to perform the sign-out operation. public SignOutResult(IList authenticationSchemes, AuthenticationProperties properties) - { - if (authenticationSchemes == null) - { - throw new ArgumentNullException(nameof(authenticationSchemes)); - } - - AuthenticationSchemes = authenticationSchemes; + { + AuthenticationSchemes = authenticationSchemes ?? throw new ArgumentNullException(nameof(authenticationSchemes)); Properties = properties; } diff --git a/src/Mvc/Mvc.Core/src/TypeFilterAttribute.cs b/src/Mvc/Mvc.Core/src/TypeFilterAttribute.cs index a2048512a6..eeef33c374 100644 --- a/src/Mvc/Mvc.Core/src/TypeFilterAttribute.cs +++ b/src/Mvc/Mvc.Core/src/TypeFilterAttribute.cs @@ -34,12 +34,7 @@ namespace Microsoft.AspNetCore.Mvc /// The of filter to create. public TypeFilterAttribute(Type type) { - if (type == null) - { - throw new ArgumentNullException(nameof(type)); - } - - ImplementationType = type; + ImplementationType = type ?? throw new ArgumentNullException(nameof(type)); } /// diff --git a/src/Mvc/Mvc.Core/src/VirtualFileResult.cs b/src/Mvc/Mvc.Core/src/VirtualFileResult.cs index b7743795a0..af075b5a09 100644 --- a/src/Mvc/Mvc.Core/src/VirtualFileResult.cs +++ b/src/Mvc/Mvc.Core/src/VirtualFileResult.cs @@ -43,12 +43,7 @@ namespace Microsoft.AspNetCore.Mvc public VirtualFileResult(string fileName, MediaTypeHeaderValue contentType) : base(contentType?.ToString()) { - if (fileName == null) - { - throw new ArgumentNullException(nameof(fileName)); - } - - FileName = fileName; + FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); } /// @@ -56,19 +51,8 @@ namespace Microsoft.AspNetCore.Mvc /// public string FileName { - get - { - return _fileName; - } - set - { - if (value == null) - { - throw new ArgumentNullException(nameof(value)); - } - - _fileName = value; - } + get => _fileName; + set => _fileName = value ?? throw new ArgumentNullException(nameof(value)); } /// diff --git a/src/Shared/PropertyActivator/PropertyActivator.cs b/src/Shared/PropertyActivator/PropertyActivator.cs index 925f6a76ae..b1d730efaf 100644 --- a/src/Shared/PropertyActivator/PropertyActivator.cs +++ b/src/Shared/PropertyActivator/PropertyActivator.cs @@ -15,19 +15,9 @@ namespace Microsoft.Extensions.Internal public PropertyActivator( PropertyInfo propertyInfo, Func valueAccessor) - { - if (propertyInfo == null) - { - throw new ArgumentNullException(nameof(propertyInfo)); - } - - if (valueAccessor == null) - { - throw new ArgumentNullException(nameof(valueAccessor)); - } - - PropertyInfo = propertyInfo; - _valueAccessor = valueAccessor; + { + PropertyInfo = propertyInfo ?? throw new ArgumentNullException(nameof(propertyInfo)); + _valueAccessor = valueAccessor ?? throw new ArgumentNullException(nameof(valueAccessor)); _fastPropertySetter = PropertyHelper.MakeFastPropertySetter(propertyInfo); } @@ -107,4 +97,4 @@ namespace Microsoft.Extensions.Internal return properties.Select(createActivateInfo).ToArray(); } } -} \ No newline at end of file +} diff --git a/src/Shared/PropertyHelper/PropertyHelper.cs b/src/Shared/PropertyHelper/PropertyHelper.cs index f3641e03dc..3349833264 100644 --- a/src/Shared/PropertyHelper/PropertyHelper.cs +++ b/src/Shared/PropertyHelper/PropertyHelper.cs @@ -51,13 +51,8 @@ namespace Microsoft.Extensions.Internal /// This constructor does not cache the helper. For caching, use . /// public PropertyHelper(PropertyInfo property) - { - if (property == null) - { - throw new ArgumentNullException(nameof(property)); - } - - Property = property; + { + Property = property ?? throw new ArgumentNullException(nameof(property)); Name = property.Name; }