Fix lgtm issues (#8843)

Add some null checks
Remove unused code
This commit is contained in:
Ilya
2019-02-13 16:51:05 +05:00
committed by GitHub
parent 68f0ae5d4e
commit ff3edaa2e3
14 changed files with 43 additions and 72 deletions
@@ -1451,7 +1451,7 @@ namespace Microsoft.PowerShell.Commands
}
else
{
if (nameMatcher.IsMatch(drive.Name))
if (nameMatcher != null && nameMatcher.IsMatch(drive.Name))
addDrive = true;
}
@@ -1193,29 +1193,24 @@ namespace Microsoft.PowerShell.Commands
content = psBody.BaseObject;
}
if (content is FormObject)
if (content is FormObject form)
{
FormObject form = content as FormObject;
SetRequestContent(request, form.Fields);
}
else if (content is IDictionary && request.Method != HttpMethod.Get)
else if (content is IDictionary dictionary && request.Method != HttpMethod.Get)
{
IDictionary dictionary = content as IDictionary;
SetRequestContent(request, dictionary);
}
else if (content is XmlNode)
else if (content is XmlNode xmlNode)
{
XmlNode xmlNode = content as XmlNode;
SetRequestContent(request, xmlNode);
}
else if (content is Stream)
else if (content is Stream stream)
{
Stream stream = content as Stream;
SetRequestContent(request, stream);
}
else if (content is byte[])
else if (content is byte[] bytes)
{
byte[] bytes = content as byte[];
SetRequestContent(request, bytes);
}
else if (content is MultipartFormDataContent multipartFormDataContent)
@@ -1225,7 +1220,8 @@ namespace Microsoft.PowerShell.Commands
}
else
{
SetRequestContent(request,
SetRequestContent(
request,
(string)LanguagePrimitives.ConvertTo(content, typeof(string), CultureInfo.InvariantCulture));
}
}
@@ -364,7 +364,7 @@ namespace Microsoft.PowerShell.Commands
{
string label = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string;
string alignment = p.GetEntry(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey) as string;
// Accept the width both as a string and as an int.
string width;
int? widthNum = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as int?;
@@ -542,7 +542,6 @@ namespace Microsoft.PowerShell.Commands
WriteObject("<table>");
_isTHWritten = false;
_propertyCollector = new StringCollection();
}
/// <summary>
@@ -760,7 +759,6 @@ namespace Microsoft.PowerShell.Commands
/// List of incoming objects to compare.
/// </summary>
private bool _isTHWritten;
private StringCollection _propertyCollector;
private List<MshParameter> _propertyMshParameterList;
private List<MshParameter> _resolvedNameMshParameters;
// private string ResourcesBaseName = "ConvertHTMLStrings";
@@ -692,21 +692,19 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
int cellCount; // scratch variable
foreach (PacketInfoData o in objects)
{
FormatEntryData fed = o as FormatEntryData;
if (fed == null)
continue;
TableRowEntry tre = fed.formatEntryInfo as TableRowEntry;
int kk = 0;
foreach (FormatPropertyField fpf in tre.formatPropertyFieldList)
if (o is FormatEntryData fed)
{
cellCount = _lo.DisplayCells.Length(fpf.propertyValue);
if (widths[kk] < cellCount)
widths[kk] = cellCount;
TableRowEntry tre = fed.formatEntryInfo as TableRowEntry;
int kk = 0;
kk++;
foreach (FormatPropertyField fpf in tre.formatPropertyFieldList)
{
cellCount = _lo.DisplayCells.Length(fpf.propertyValue);
if (widths[kk] < cellCount)
widths[kk] = cellCount;
kk++;
}
}
}
@@ -729,19 +727,17 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
foreach (PacketInfoData o in objects)
{
FormatEntryData fed = o as FormatEntryData;
if (fed == null)
continue;
WideViewEntry wve = fed.formatEntryInfo as WideViewEntry;
FormatPropertyField fpf = wve.formatPropertyField as FormatPropertyField;
if (!string.IsNullOrEmpty(fpf.propertyValue))
if (o is FormatEntryData fed)
{
cellCount = _lo.DisplayCells.Length(fpf.propertyValue);
if (cellCount > maxLen)
maxLen = cellCount;
WideViewEntry wve = fed.formatEntryInfo as WideViewEntry;
FormatPropertyField fpf = wve.formatPropertyField as FormatPropertyField;
if (!string.IsNullOrEmpty(fpf.propertyValue))
{
cellCount = _lo.DisplayCells.Length(fpf.propertyValue);
if (cellCount > maxLen)
maxLen = cellCount;
}
}
}
@@ -226,10 +226,6 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
PSPropertyExpressionResult expressionResult;
bool retVal = DisplayCondition.Evaluate(currentObject, ex, out expressionResult);
if (expressionResult != null && expressionResult.Exception != null)
{
_failedResultsList.Add(expressionResult);
}
return retVal;
}
@@ -239,8 +235,6 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
private Collection<string> _typeNameHierarchy;
private bool _useInheritance;
private List<PSPropertyExpressionResult> _failedResultsList = new List<PSPropertyExpressionResult>();
private int _bestMatchIndex = BestMatchIndexUndefined;
private TypeMatchItem _bestMatchItem;
@@ -237,9 +237,6 @@ function __cmdletization_BindCommonParameters
return verb + "-" + noun;
}
private readonly List<string> _aliasesToExport = new List<string>();
private readonly List<string> _functionsToExport = new List<string>();
private string GetCmdletAttributes(CommonCmdletMetadata cmdletMetadata)
{
// Generate the script for the Alias and Obsolete Attribute if any is declared in CDXML
@@ -247,7 +244,6 @@ function __cmdletization_BindCommonParameters
if (cmdletMetadata.Aliases != null)
{
attributes.Append("[Alias('" + string.Join("','", cmdletMetadata.Aliases.Select(alias => CodeGeneration.EscapeSingleQuotedStringContent(alias))) + "')]");
_aliasesToExport.AddRange(cmdletMetadata.Aliases);
}
if (cmdletMetadata.Obsolete != null)
@@ -1924,8 +1920,6 @@ Microsoft.PowerShell.Core\Export-ModuleMember -Function '{1}' -Alias '*'
CmdletEndBlockTemplate,
/* 0 */ this.GetHelpDirectiveForExternalHelp(),
/* 1 */ CodeGeneration.EscapeSingleQuotedStringContent(commandMetadata.Name));
_functionsToExport.Add(commandMetadata.Name);
}
private static void AddPassThruParameter(IDictionary<string, ParameterMetadata> commonParameters, InstanceCmdletMetadata instanceCmdletMetadata)
@@ -2022,8 +2016,6 @@ Microsoft.PowerShell.Core\Export-ModuleMember -Function '{1}' -Alias '*'
CmdletEndBlockTemplate,
/* 0 */ this.GetHelpDirectiveForExternalHelp(),
/* 1 */ CodeGeneration.EscapeSingleQuotedStringContent(commandMetadata.Name));
_functionsToExport.Add(commandMetadata.Name);
}
private string GetOutputAttributeForGetCmdlet()
@@ -2109,8 +2101,6 @@ Microsoft.PowerShell.Core\Export-ModuleMember -Function '{1}' -Alias '*'
CmdletEndBlockTemplate,
/* 0 */ this.GetHelpDirectiveForExternalHelp(),
/* 1 */ CodeGeneration.EscapeSingleQuotedStringContent(commandMetadata.Name));
_functionsToExport.Add(commandMetadata.Name);
}
private static object s_enumCompilationLock = new object();
@@ -3793,7 +3793,7 @@ namespace System.Management.Automation
FieldInfo field = adapterData.member as FieldInfo;
if (adapterData.useReflection)
{
return field.GetValue(property.baseObject);
return field?.GetValue(property.baseObject);
}
else
{
@@ -1220,8 +1220,7 @@ namespace System.Management.Automation.Runspaces
{
foreach (var element in items)
{
var typeEntry = element as SessionStateTypeEntry;
if (typeEntry.TypeData != null)
if (element is SessionStateTypeEntry typeEntry && typeEntry.TypeData != null)
{
// Skip type file entries.
_internalCollection.Add(element);
@@ -1744,7 +1744,7 @@ namespace System.Management.Automation
// Update the prompt to the debug prompt
if (hadDefaultPrompt)
{
int index = originalPromptString.IndexOf("\"", StringComparison.OrdinalIgnoreCase);
int index = originalPromptString.IndexOf('"', StringComparison.OrdinalIgnoreCase);
if (index > -1)
{
// Fix up prompt.
@@ -3317,9 +3317,8 @@ namespace Microsoft.PowerShell.Commands
try
{
if (ShouldUseSteppablePipelineOnServer)
if (ShouldUseSteppablePipelineOnServer && pipeline is RemotePipeline rPipeline)
{
RemotePipeline rPipeline = pipeline as RemotePipeline;
rPipeline.SetIsNested(true);
rPipeline.SetIsSteppable(true);
}
@@ -63,9 +63,8 @@ namespace System.Management.Automation.Help
obj.TypeNames.Add("CmdletHelpInfo");
obj.TypeNames.Add("HelpInfo");
if (commandInfo is CmdletInfo)
if (commandInfo is CmdletInfo cmdletInfo)
{
CmdletInfo cmdletInfo = commandInfo as CmdletInfo;
bool common = false;
if (cmdletInfo.Parameters != null)
{
@@ -101,9 +100,8 @@ namespace System.Management.Automation.Help
obj.Properties.Add(new PSNoteProperty("PSSnapIn", cmdletInfo.PSSnapIn));
}
else if (commandInfo is FunctionInfo)
else if (commandInfo is FunctionInfo funcInfo)
{
FunctionInfo funcInfo = commandInfo as FunctionInfo;
bool common = HasCommonParameters(funcInfo.Parameters);
obj.Properties.Add(new PSNoteProperty("CommonParameters", common));
@@ -392,7 +392,6 @@ namespace System.Management.Automation
/// <remarks>helpInfos is not null or empty.</remarks>
private IEnumerable<HelpInfo> ForwardHelp(HelpInfo helpInfo, HelpRequest helpRequest)
{
Collection<HelpInfo> result = new Collection<HelpInfo>();
// findout if this helpInfo needs to be processed further..
if (helpInfo.ForwardHelpCategory == HelpCategory.None && string.IsNullOrEmpty(helpInfo.ForwardTarget))
{
@@ -823,10 +823,10 @@ namespace Microsoft.PowerShell.Commands
/// <param name="message">Message to log.</param>
internal void LogMessage(string message)
{
#if !CORECLR // TODO:CORECLR Uncomment when we add PSEtwLog support
List<string> details = new List<string>();
details.Add(message);
#if !CORECLR // TODO:CORECLR Uncomment when we add PSEtwLog support
PSEtwLog.LogPipelineExecutionDetailEvent(MshLog.GetLogContext(Context, Context.CurrentCommandProcessor.Command.MyInvocation), details);
#endif
}
@@ -2136,7 +2136,7 @@ namespace Microsoft.PowerShell.Commands
if (ShouldProcess(resource, action))
{
bool isDirectory = false;
string strTargetPath = value.ToString();
string strTargetPath = value?.ToString();
if (string.IsNullOrEmpty(strTargetPath))
{
@@ -2310,7 +2310,7 @@ namespace Microsoft.PowerShell.Commands
if (ShouldProcess(resource, action))
{
bool isDirectory = false;
string strTargetPath = value.ToString();
string strTargetPath = value?.ToString();
bool exists = false;
@@ -4383,7 +4383,7 @@ namespace Microsoft.PowerShell.Commands
fStream = AlternateDataStreamUtilities.CreateFileStream(file.FullName, streamName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
#endif
long remainingFileSize = fStream.Length;
long remainingFileSize = fStream != null ? fStream.Length : 0;
do
{
if (Stopping)
@@ -8071,8 +8071,10 @@ namespace Microsoft.PowerShell.Commands
targetDir = Encoding.Unicode.GetString(reparseDataBufferMountPoint.PathBuffer, reparseDataBufferMountPoint.SubstituteNameOffset, reparseDataBufferMountPoint.SubstituteNameLength);
}
if (targetDir.StartsWith(NonInterpretedPathPrefix, StringComparison.OrdinalIgnoreCase))
if (targetDir != null && targetDir.StartsWith(NonInterpretedPathPrefix, StringComparison.OrdinalIgnoreCase))
{
targetDir = targetDir.Substring(NonInterpretedPathPrefix.Length);
}
return targetDir;
}