Skip to content

Commit

Permalink
Code redundancy fixes (PowerShell#12916)
Browse files Browse the repository at this point in the history
* Autofix `RCS1036: Remove redundant empty line`

* Autofix `RCS1037: Remove trailing white-space`

* Autofix `RCS1033: Remove redundant boolean literal`

* Autofix: `RCS1038: Remove empty statement`

* Autofix `RCS1041: Remove empty initializer`

* Autofix `RCS1097: Remove redundant 'ToString' call

* Autofix: `RCS1420: Operator is unnecessary`
  • Loading branch information
xtqqczze authored Jun 11, 2020
1 parent eaee9a3 commit 0d5d017
Show file tree
Hide file tree
Showing 95 changed files with 160 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ private void CloneParameterEntries(
/// </summary>
public class CimBaseCommand : Cmdlet, IDisposable
{

#region resolve parameter set name
/// <summary>
/// <para>
Expand Down Expand Up @@ -982,7 +981,6 @@ internal CimCredential CreateCimCredentials(PSCredential psCredentials,
NetworkCredential networkCredential = psCredentials.GetNetworkCredential();
DebugHelper.WriteLog("Domain:{0}; UserName:{1}; Password:{2}.", 1, networkCredential.Domain, networkCredential.UserName, psCredentials.Password);
credentials = new CimCredential(passwordAuthentication, networkCredential.Domain, networkCredential.UserName, psCredentials.Password);

}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public void NewCimInstance(NewCimInstanceCommand cmdlet)
cmdlet.Key,
cmdlet.Property,
cmdlet);

}

break;
Expand All @@ -92,7 +91,6 @@ public void NewCimInstance(NewCimInstanceCommand cmdlet)
cmdlet.Key,
cmdlet.Property,
cmdlet);

}

break;
Expand All @@ -103,7 +101,6 @@ public void NewCimInstance(NewCimInstanceCommand cmdlet)
cimInstance = CreateCimInstance(cmdlet.CimClass,
cmdlet.Property,
cmdlet);

}

break;
Expand Down Expand Up @@ -313,7 +310,6 @@ private CimInstance CreateCimInstance(
flag);
cimInstance.CimInstanceProperties.Add(newProperty);
}

}

return cimInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace Microsoft.Management.Infrastructure.CimCmdlets
{

/// <summary>
/// <para>
/// Subscription result event args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ internal object Result
private object result;
#endregion
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ private void CheckArgument()
new ParameterDefinitionEntry(CimBaseCommand.QueryComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, false),

}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ private static void WriteLogInternal(string message, int indent, int depth)
{
writer.WriteLineAsync(spaces[indent] + sourceInformation + @" " + message);
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ private static void GetRoundedValues(DateTime value1, DateTime value2, out DateT

private static bool HasTimeComponent(DateTime value)
{
bool hasNoTimeComponent = true
&& value.Hour == 0
bool hasNoTimeComponent = value.Hour == 0
&& value.Minute == 0
&& value.Second == 0
&& value.Millisecond == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static partial void IsTextTrimmedMonitoringEnabledProperty_PropertyChangedImplem
return;
}

if ((bool)e.OldValue == true)
if ((bool)e.OldValue)
{
tb.SizeChanged -= OnTextBlockSizeChanged;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ public override string GetErrorMessageForInvalidValue(string value, Type typeToP
throw new ArgumentNullException("typeToParseTo");
}

bool isNumericType = false
|| typeToParseTo == typeof(byte)
bool isNumericType = typeToParseTo == typeof(byte)
|| typeToParseTo == typeof(sbyte)
|| typeToParseTo == typeof(short)
|| typeToParseTo == typeof(ushort)
Expand Down
2 changes: 0 additions & 2 deletions src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ uint dwFlags
[DllImport(LibraryLoadDllName)]
private static extern bool FreeLibrary(IntPtr hModule);


[DllImport(LocalizationDllName, EntryPoint = "GetUserDefaultLangID", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
private static extern ushort GetUserDefaultLangID();


public static uint FormatMessageFromModule(uint lastError, string moduleName, out String msg)
{
Debug.Assert(!string.IsNullOrEmpty(moduleName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ public string[] ComputerName
//
protected override void BeginProcessing()
{

if (Platform.IsIoT)
{
// IoT does not have the '$env:windir\System32\pdh.dll' assembly which is required by this cmdlet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,6 @@ private void AddLogsForProviderToInternalMap(EventLogSession eventLogSession, st
_providersByLogMap.Add(logLink.LogName.ToLowerInvariant(), provColl);
}
else

{
//
// Log is there: add provider, if needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal override IObservable<CimInstance> GetCimOperation()
else
{
#if DEBUG
Dbg.Assert(_createInstanceOperationGotStarted == true, "GetInstance should be started *after* CreateInstance");
Dbg.Assert(_createInstanceOperationGotStarted, "GetInstance should be started *after* CreateInstance");
Dbg.Assert(_getInstanceOperationGotStarted == false, "Should not start GetInstance operation twice");
Dbg.Assert(_resultFromGetInstance == null, "GetInstance operation shouldn't happen twice");
_getInstanceOperationGotStarted = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ internal static bool IsValidDrive(string drive)
{
if (logicalDrive.DriveType.Equals(DriveType.Fixed))
{
if (drive.ToString().Equals(logicalDrive.Name.ToString(), System.StringComparison.OrdinalIgnoreCase))
if (drive.Equals(logicalDrive.Name, System.StringComparison.OrdinalIgnoreCase))
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public sealed class GetHotFixCommand : PSCmdlet, IDisposable
private ManagementObjectSearcher _searchProcess;

private bool _inputContainsWildcard = false;
private readonly ConnectionOptions _connectionOptions = new ConnectionOptions { };
private readonly ConnectionOptions _connectionOptions = new ConnectionOptions();

/// <summary>
/// Sets connection options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ internal void SetServiceSecurityDescriptor(
StringUtil.Format(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl, service.ServiceName, exception.Message),
accessDenied ? ErrorCategory.PermissionDenied : ErrorCategory.InvalidOperation);
}

}
#endregion Internal
}
Expand Down Expand Up @@ -2749,7 +2748,6 @@ NakedWin32Handle CreateServiceW(
[In] IntPtr lpPassword
);


[DllImport(PinvokeDllNames.SetServiceObjectSecurityDllName, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ protected override void EndProcessing()
return;
}


if (_loadAssembly)
{
// File extension is ".DLL" (ParameterSetName = FromPathParameterSetName or FromLiteralPathParameterSetName).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ internal static char SetDelimiter(PSCmdlet cmdlet, string parameterSetName, char
case "UseCulture":
case "CulturePath":
case "CultureLiteralPath":
if (useCulture == true)
if (useCulture)
{
// ListSeparator is apparently always a character even though the property returns a string, checked via:
// [CultureInfo]::GetCultures("AllCultures") | % { ([CultureInfo]($_.Name)).TextInfo.ListSeparator } | ? Length -ne 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ private void WriteStartOfPSObject(
if (property != null)
{
WriteStartElement(_writer, CustomSerializationStrings.Properties);
WriteAttribute(_writer, CustomSerializationStrings.NameAttribute, property.ToString());
WriteAttribute(_writer, CustomSerializationStrings.NameAttribute, property);
}
else
{
Expand Down Expand Up @@ -1063,9 +1063,9 @@ private void WritePropertyWithNullValue(
XmlWriter writer, PSPropertyInfo source, int depth)
{
WriteStartElement(writer, CustomSerializationStrings.Properties);
WriteAttribute(writer, CustomSerializationStrings.NameAttribute, ((PSPropertyInfo)source).Name.ToString());
WriteAttribute(writer, CustomSerializationStrings.NameAttribute, ((PSPropertyInfo)source).Name);
if (!_notypeinformation)
WriteAttribute(writer, CustomSerializationStrings.TypeAttribute, ((PSPropertyInfo)source).TypeNameOfValue.ToString());
WriteAttribute(writer, CustomSerializationStrings.TypeAttribute, ((PSPropertyInfo)source).TypeNameOfValue);
writer.WriteEndElement();
}

Expand All @@ -1075,7 +1075,7 @@ private void WriteObjectString(
if (property != null)
{
WriteStartElement(writer, CustomSerializationStrings.Properties);
WriteAttribute(writer, CustomSerializationStrings.NameAttribute, property.ToString());
WriteAttribute(writer, CustomSerializationStrings.NameAttribute, property);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ protected override void ProcessRecord()

// During remoting, remain compatible with v5.0- clients by default.
// Passing a -PowerShellVersion argument allows overriding the client version.
bool writeOldWay =
bool writeOldWay =
(remotingClientInfo != null && clientVersion == null) // To be safe: Remoting client version could unexpectedly not be determined.
||
||
(clientVersion != null
&&
(clientVersion.Major < 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

using Dbg = System.Management.Automation.Diagnostics;


namespace Microsoft.PowerShell.Commands
{
using PowerShell = System.Management.Automation.PowerShell;
Expand Down Expand Up @@ -800,7 +799,7 @@ private void ReportSkippedCommands()
if (_commandsSkippedBecauseOfShadowing.Count != 0)
{
string skippedCommands = string.Join(", ", _commandsSkippedBecauseOfShadowing.ToArray());
ErrorRecord errorRecord = this.GetErrorCommandSkippedBecauseOfShadowing(skippedCommands.ToString());
ErrorRecord errorRecord = this.GetErrorCommandSkippedBecauseOfShadowing(skippedCommands);
this.WriteWarning(errorRecord.ErrorDetails.Message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ private object CreateComObject()

thread.Join();

if (createInfo.success == true)
if (createInfo.success)
{
return createInfo.objectCreated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ protected override void EndProcessing()
finally
{
_mSmtpClient.Dispose();

// If we don't dispose the attachments, the sender can't modify or use the files sent.
_mMailMessage.Attachments.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public enum TextEncodingType
/// Big Endian UTF32 encoding.
/// </summary>
BigEndianUTF32,

/// <summary>
/// UTF8 encoding.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ internal virtual HttpRequestMessage GetRequest(Uri uri)
if (!string.IsNullOrEmpty(CustomMethod))
{
// set the method if the parameter was provided
httpMethod = new HttpMethod(CustomMethod.ToString().ToUpperInvariant());
httpMethod = new HttpMethod(CustomMethod.ToUpperInvariant());
}

break;
Expand Down Expand Up @@ -1478,7 +1478,7 @@ protected override void ProcessRecord()
&&
PreserveAuthorizationOnRedirect.IsPresent
&&
WebSession.Headers.ContainsKey(HttpKnownHeaderNames.Authorization.ToString());
WebSession.Headers.ContainsKey(HttpKnownHeaderNames.Authorization);

using (HttpClient client = GetHttpClient(keepAuthorization))
{
Expand Down Expand Up @@ -1845,7 +1845,7 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr
if (url != string.Empty && rel != string.Empty && !_relationLink.ContainsKey(rel))
{
Uri absoluteUri = new Uri(requestUri, url);
_relationLink.Add(rel, absoluteUri.AbsoluteUri.ToString());
_relationLink.Add(rel, absoluteUri.AbsoluteUri);
}
}
}
Expand Down Expand Up @@ -1899,7 +1899,7 @@ private void AddMultipartContent(object fieldName, object fieldValue, MultipartF
}

// Treat the value as a collection and enumerate it if enumeration is true
if (enumerate == true && fieldValue is IEnumerable items)
if (enumerate && fieldValue is IEnumerable items)
{
foreach (var item in items)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ private static object ProcessCustomObject<T>(object o, int depth, in ConvertToJs
if (!info2.IsDefined(typeof(T), true))
{
MethodInfo getMethod = info2.GetGetMethod();
if ((getMethod != null) && (getMethod.GetParameters().Length <= 0))
if ((getMethod != null) && (getMethod.GetParameters().Length == 0))
{
object value;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private string ProcessObject(object o)

foreach (object element in enumerable)
{
if (printSeparator == true && Separator != null)
if (printSeparator && Separator != null)
{
result.Append(Separator.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,6 @@ private void ParseHelper(string[] args)
{
// Just toss this option, it was processed earlier...
}

else if (MatchSwitch(switchKey, "modules", "mod"))
{
if (ConsoleHost.DefaultInitialSessionState == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ internal bool ShouldEndSession
{
// If ShouldEndSession is already true, you can't set it back

Dbg.Assert(_shouldEndSession != true || value != false,
Dbg.Assert(_shouldEndSession != true || value,
"ShouldEndSession can only be set from false to true");

_shouldEndSession = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace Microsoft.PowerShell
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
internal partial class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInterface
{

/// <summary>
/// This is the char that is echoed to the console when the input is masked. This not localizable.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ internal void ExecuteCommandAsyncHelper(Pipeline tempPipeline, out Exception exc
// hence the Input pipe.
break;
}
};
}
des.End();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public static void EvtSetChannelConfigProperty(EventLogHandle handle, UnsafeNati
case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigEnabled:
{
varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
if ((bool)val == true) varVal.Bool = 1;
if ((bool)val) varVal.Bool = 1;
else varVal.Bool = 0;
}

Expand Down Expand Up @@ -730,15 +730,15 @@ public static void EvtSetChannelConfigProperty(EventLogHandle handle, UnsafeNati
case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention:
{
varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
if ((bool)val == true) varVal.Bool = 1;
if ((bool)val) varVal.Bool = 1;
else varVal.Bool = 0;
}

break;
case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup:
{
varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
if ((bool)val == true) varVal.Bool = 1;
if ((bool)val) varVal.Bool = 1;
else varVal.Bool = 0;
}

Expand Down
Loading

0 comments on commit 0d5d017

Please sign in to comment.