From 74dd15c868f03fa01d1486edc4d5e30f5b5ab8a4 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra <13396919+cheenamalhotra@users.noreply.github.com> Date: Thu, 16 Feb 2023 16:15:30 -0800 Subject: [PATCH] Address error IDE0270 after MsBuild update (#1865) --- .../ReliableConnection/ReliableSqlCommand.cs | 8 +---- .../Firewall/FirewallRuleService.cs | 8 ++--- .../Agent/Jobs/AgentOperatorData.cs | 6 +--- .../DacFx/ValidateStreamingJobOperation.cs | 8 +---- .../ExecutionPlan/ShowPlan/PropertyFactory.cs | 16 ++-------- .../ShowPlan/XmlPlanNodeBuilder.cs | 8 ++--- .../Impl/SqlTableDefinitionFormatter.cs | 9 ++---- .../Management/Common/Utils.cs | 32 +++++-------------- .../ObjectExplorer/Nodes/NodeTypes.cs | 2 +- .../SmoModel/SmoQueryContext.cs | 10 ++---- .../Profiler/ProfilerService.cs | 7 +--- .../QueryExecution/Query.cs | 6 +--- ...chemaCompareIncludeExcludeNodeOperation.cs | 7 +--- .../Security/UserData.cs | 6 +--- 14 files changed, 27 insertions(+), 106 deletions(-) diff --git a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableSqlCommand.cs b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableSqlCommand.cs index 278ad0ec..c8738a27 100644 --- a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableSqlCommand.cs +++ b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableSqlCommand.cs @@ -122,13 +122,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection throw new ArgumentNullException("value"); } - ReliableSqlConnection newConnection = value as ReliableSqlConnection; - - if (newConnection == null) - { - throw new InvalidOperationException(Resources.OnlyReliableConnectionSupported); - } - + ReliableSqlConnection newConnection = value as ReliableSqlConnection ?? throw new InvalidOperationException(Resources.OnlyReliableConnectionSupported); _connection = newConnection; _command.Connection = _connection._underlyingConnection; } diff --git a/src/Microsoft.SqlTools.ResourceProvider.Core/Firewall/FirewallRuleService.cs b/src/Microsoft.SqlTools.ResourceProvider.Core/Firewall/FirewallRuleService.cs index 17bf6ce5..c0888f74 100644 --- a/src/Microsoft.SqlTools.ResourceProvider.Core/Firewall/FirewallRuleService.cs +++ b/src/Microsoft.SqlTools.ResourceProvider.Core/Firewall/FirewallRuleService.cs @@ -159,12 +159,8 @@ namespace Microsoft.SqlTools.ResourceProvider.Core.Firewall { try { - IEnumerable subscriptions = await AuthenticationManager.GetSubscriptionsAsync(); - - if (subscriptions == null) - { - throw new FirewallRuleException(SR.NoSubscriptionsFound); - } + IEnumerable subscriptions = await AuthenticationManager.GetSubscriptionsAsync() + ?? throw new FirewallRuleException(SR.NoSubscriptionsFound); ServiceResponse response = await AzureUtil.ExecuteGetAzureResourceAsParallel((object)null, subscriptions, serverName, new CancellationToken(), TryFindAzureResourceForSubscriptionAsync); diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs index b695363d..26e09aa4 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs @@ -803,11 +803,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent /// Job server object private JobServer GetJobServer() { - JobServer jobServer = this.dataContainer.Server.JobServer; - if (jobServer == null) - { - throw new ApplicationException(SR.JobServerIsNotAvailable); - } + JobServer jobServer = this.dataContainer.Server.JobServer ?? throw new ApplicationException(SR.JobServerIsNotAvailable); return jobServer; } diff --git a/src/Microsoft.SqlTools.ServiceLayer/DacFx/ValidateStreamingJobOperation.cs b/src/Microsoft.SqlTools.ServiceLayer/DacFx/ValidateStreamingJobOperation.cs index af4dba3a..12fcb5cc 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/DacFx/ValidateStreamingJobOperation.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/DacFx/ValidateStreamingJobOperation.cs @@ -104,14 +104,8 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx TSqlBatch batch = ((TSqlScript)fragment).Batches[0]; TSqlStatement statement = batch.Statements[0]; - CreateExternalStreamingJobStatement createStatement = statement as CreateExternalStreamingJobStatement; - // if the TSQL doesn't contain a CreateExternalStreamingJobStatement, we're in a bad path. - - if (createStatement == null) - { - throw new ArgumentException(SR.NoCreateStreamingJobStatementFound); - } + CreateExternalStreamingJobStatement createStatement = statement as CreateExternalStreamingJobStatement ?? throw new ArgumentException(SR.NoCreateStreamingJobStatementFound); return (createStatement.Name.Value, createStatement.Statement.Value); } diff --git a/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/PropertyFactory.cs b/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/PropertyFactory.cs index 0d91054a..3463070c 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/PropertyFactory.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/PropertyFactory.cs @@ -756,20 +756,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ExecutionPlan.ShowPlan private static IEnumerable GetAttributeCollectionForChoiceElement(PropertyDescriptor property) { Type type = property.ComponentType; - PropertyInfo pInfo = type.GetProperty("Items"); - - if (pInfo == null) - { - //Try using item. - pInfo = type.GetProperty("Item"); - } - - if (pInfo != null) - { - return pInfo.GetCustomAttributes(true); - } - - return property.Attributes; + PropertyInfo pInfo = type.GetProperty("Items") ?? type.GetProperty("Item"); + return pInfo != null ? pInfo.GetCustomAttributes(true) : property.Attributes; } public static PropertyDescriptor CreateProperty(string propertyName, object value) { diff --git a/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/XmlPlanNodeBuilder.cs b/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/XmlPlanNodeBuilder.cs index 8e3df51e..7f9588f9 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/XmlPlanNodeBuilder.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ExecutionPlan/ShowPlan/XmlPlanNodeBuilder.cs @@ -384,12 +384,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ExecutionPlan.ShowPlan nsMgr.AddNamespace("shp", "http://schemas.microsoft.com/sqlserver/2004/07/showplan"); //The root node in this case is the statement node - XmlNode rootNode = stmtXmlDocument.DocumentElement; - if(rootNode == null) - { - //Couldn't find our statement node, this should never happen in a properly formed document - throw new ArgumentNullException("StatementNode"); - } + //If couldn't find our statement node, throw exception, as this should never happen in a properly formed document + XmlNode rootNode = stmtXmlDocument.DocumentElement ?? throw new ArgumentNullException("StatementNode"); XmlNode missingIndexes = rootNode.SelectSingleNode("descendant::shp:MissingIndexes", nsMgr); diff --git a/src/Microsoft.SqlTools.ServiceLayer/Formatter/Impl/SqlTableDefinitionFormatter.cs b/src/Microsoft.SqlTools.ServiceLayer/Formatter/Impl/SqlTableDefinitionFormatter.cs index 8d1b1480..3b0233cd 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Formatter/Impl/SqlTableDefinitionFormatter.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Formatter/Impl/SqlTableDefinitionFormatter.cs @@ -22,7 +22,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Formatter return new SqlTableDefinitionFormatter(visitor, codeObject); } } - + internal class SqlTableDefinitionFormatter : ASTNodeFormatterT { private CommaSeparatedListFormatter CommaSeparatedListFormatter { get; set; } @@ -42,12 +42,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Formatter { if (child is SqlColumnDefinition && !(child is SqlComputedColumnDefinition)) { - SqlIdentifier identifierChild = child.Children.ElementAtOrDefault(0) as SqlIdentifier; - - if (identifierChild == null) - { - throw new FormatFailedException("unexpected token at index start Token Index"); - } + SqlIdentifier identifierChild = child.Children.ElementAtOrDefault(0) as SqlIdentifier ?? throw new FormatFailedException("unexpected token at index start Token Index"); string s1 = child.TokenManager.GetText(identifierChild.Position.startTokenNumber, identifierChild.Position.endTokenNumber); range1MaxLength = Math.Max(range1MaxLength, s1.Length); diff --git a/src/Microsoft.SqlTools.ServiceLayer/Management/Common/Utils.cs b/src/Microsoft.SqlTools.ServiceLayer/Management/Common/Utils.cs index aba0815b..508881ee 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Management/Common/Utils.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Management/Common/Utils.cs @@ -31,7 +31,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Management /// /// public static Attribute GetCustomAttribute(object objectToGetAttributeFrom, Type customAttribute) - { + { //first, see if the object implemented this interface to override standard behavior System.Reflection.ICustomAttributeProvider attribProvider = objectToGetAttributeFrom as System.Reflection.ICustomAttributeProvider; //if not, get it from its type @@ -57,22 +57,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Management /// public static SqlConnectionInfo GetSqlConnectionInfoFromDataContainer(CDataContainer dc) { - if (dc != null) - { - // we may have been given conneciton information by the object explorer. in which case there is no need - // to build it ourselves. - SqlConnectionInfo result = dc.ConnectionInfo as SqlConnectionInfo; - if (result == null) - { - throw new InvalidOperationException(); - } - - return result; - } - else - { - return null; - } + // we may have been given conneciton information by the object explorer. in which case there is no need + // to build it ourselves. + return dc != null ? dc.ConnectionInfo as SqlConnectionInfo ?? throw new InvalidOperationException() + : null; } public static int InitialTreeViewWidth @@ -226,7 +214,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Management bool result = false; if (svr != null) { - if (IsKatmaiOrLater(svr.Information.Version.Major) + if (IsKatmaiOrLater(svr.Information.Version.Major) && svr.ServerType != DatabaseEngineType.SqlAzureDatabase) //Azure doesn't support filestream { if (svr.Configuration.FilestreamAccessLevel.RunValue != 0) @@ -390,11 +378,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Management /// Smo Server object for the connection public static Microsoft.SqlServer.Management.Smo.Server GetSmoServer(IManagedConnection mc) { - SqlOlapConnectionInfoBase ci = mc.Connection; - if (ci == null) - { - throw new ArgumentNullException("ci"); - } + SqlOlapConnectionInfoBase ci = mc.Connection ?? throw new ArgumentNullException("ci"); SMO.Server server = null; @@ -579,7 +563,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Management if (string.IsNullOrWhiteSpace(s)) { return s; - } + } StringBuilder sb = new StringBuilder(s.Length * 2); foreach (char c in s) diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/Nodes/NodeTypes.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/Nodes/NodeTypes.cs index aa491571..2f2f1038 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/Nodes/NodeTypes.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/Nodes/NodeTypes.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs index 0eebef7e..e5507d32 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs @@ -118,14 +118,10 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel { throw new InvalidOperationException(SqlTools.Hosting.SR.ServiceProviderNotSet); } - ObjectExplorerService service = ServiceProvider.GetService(); - if (service == null) - { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, - SqlTools.Hosting.SR.ServiceNotFound, nameof(ObjectExplorerService))); - } - return service; + return ServiceProvider.GetService() + ?? throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, + SqlTools.Hosting.SR.ServiceNotFound, nameof(ObjectExplorerService))); } /// diff --git a/src/Microsoft.SqlTools.ServiceLayer/Profiler/ProfilerService.cs b/src/Microsoft.SqlTools.ServiceLayer/Profiler/ProfilerService.cs index b000f7c2..a59ce639 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Profiler/ProfilerService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Profiler/ProfilerService.cs @@ -308,14 +308,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler var sqlConnection = ConnectionService.OpenSqlConnection(connInfo); SqlStoreConnection connection = new SqlStoreConnection(sqlConnection); BaseXEStore store = CreateXEventStore(connInfo, connection); - Session session = store.Sessions[sessionName]; + Session session = store.Sessions[sessionName] ?? throw new Exception(SR.SessionNotFound); // start the session if it isn't already running - if (session == null) - { - throw new Exception(SR.SessionNotFound); - } - if (session != null && !session.IsRunning) { session.Start(); diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs index 03dd66bf..62cd0adc 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs @@ -580,11 +580,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// private void OnInfoMessage(object sender, SqlInfoMessageEventArgs args) { - SqlConnection conn = sender as SqlConnection; - if (conn == null) - { - throw new InvalidOperationException(SR.QueryServiceMessageSenderNotSql); - } + SqlConnection conn = sender as SqlConnection ?? throw new InvalidOperationException(SR.QueryServiceMessageSenderNotSql); foreach (SqlError error in args.Errors) { diff --git a/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareIncludeExcludeNodeOperation.cs b/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareIncludeExcludeNodeOperation.cs index c73fd7ac..0373f384 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareIncludeExcludeNodeOperation.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/SchemaCompare/SchemaCompareIncludeExcludeNodeOperation.cs @@ -64,12 +64,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare try { - SchemaDifference node = this.FindDifference(this.ComparisonResult.Differences, this.Parameters.DiffEntry); - if (node == null) - { - throw new InvalidOperationException(SR.SchemaCompareExcludeIncludeNodeNotFound); - } - + SchemaDifference node = this.FindDifference(this.ComparisonResult.Differences, this.Parameters.DiffEntry) ?? throw new InvalidOperationException(SR.SchemaCompareExcludeIncludeNodeNotFound); this.Success = this.Parameters.IncludeRequest ? this.ComparisonResult.Include(node) : this.ComparisonResult.Exclude(node); // if include request (pass or fail), send dependencies that might have been affected by this request, given by GetIncludeDependencies() diff --git a/src/Microsoft.SqlTools.ServiceLayer/Security/UserData.cs b/src/Microsoft.SqlTools.ServiceLayer/Security/UserData.cs index d09f577c..2d61ca0f 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Security/UserData.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Security/UserData.cs @@ -454,11 +454,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Security this.originalState = original; this.exists = !context.IsNewObject; - Database? parent = context.Server.GetSmoObject(new Urn(context.ParentUrn)) as Database; - if (parent == null) - { - throw new ArgumentException("Context ParentUrn is invalid"); - } + Database? parent = context.Server.GetSmoObject(new Urn(context.ParentUrn)) as Database ?? throw new ArgumentException("Context ParentUrn is invalid"); this.parent = parent; this.roleNames = this.PopulateRoles();