Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lgtm issues #8843

Merged
merged 18 commits into from
Feb 13, 2019
Merged

Fix lgtm issues #8843

merged 18 commits into from
Feb 13, 2019

Conversation

iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Feb 7, 2019

PR Summary

  • Add some null checks.
  • Remove unused code.

PR Context

Come from https://lgtm.com/projects/g/PowerShell/PowerShell/alerts/?mode=tree&ruleFocus=1506101336231

PR Checklist

@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Feb 7, 2019
@RDIL
Copy link
Contributor

RDIL commented Feb 7, 2019

It seems like this PR is making the Code factor grade decline.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Feb 7, 2019

@RDIL No, the issues already was there. CodeFactor has strange reports sometimes.

@RDIL
Copy link
Contributor

RDIL commented Feb 7, 2019

Ah okay.

Copy link
Contributor

@markekraus markekraus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Web cmdlet changes LGTM

Copy link
Member

@adityapatwardhan adityapatwardhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with non-blocking suggestions

@@ -1451,7 +1451,7 @@ public class DriveMatchingCoreCommandBase : CoreCommandBase
}
else
{
if (nameMatcher.IsMatch(drive.Name))
if (nameMatcher != null && nameMatcher.IsMatch(drive.Name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be

Suggested change
if (nameMatcher != null && nameMatcher.IsMatch(drive.Name))
if (nameMatcher?.IsMatch(drive.Name) ?? false)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Report is "Cannot implicitly convert type bool? to bool."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this works?

if (nameMatcher?.IsMatch(drive.Name) ?? false)
{
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks as puzzle :-)

@@ -8071,8 +8071,10 @@ private static string WinInternalGetTarget(SafeFileHandle handle)
targetDir = Encoding.Unicode.GetString(reparseDataBufferMountPoint.PathBuffer, reparseDataBufferMountPoint.SubstituteNameOffset, reparseDataBufferMountPoint.SubstituteNameLength);
}

if (targetDir.StartsWith(NonInterpretedPathPrefix, StringComparison.OrdinalIgnoreCase))
if (targetDir != null && targetDir.StartsWith(NonInterpretedPathPrefix, StringComparison.OrdinalIgnoreCase))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be

Suggested change
if (targetDir != null && targetDir.StartsWith(NonInterpretedPathPrefix, StringComparison.OrdinalIgnoreCase))
if (targetDir?.StartsWith(NonInterpretedPathPrefix, StringComparison.OrdinalIgnoreCase) ?? false)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Collaborator

@vexx32 vexx32 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the cleanup! 👍 😍

SetRequestContent(request, form.Fields);
}
else if (content is IDictionary && request.Method != HttpMethod.Get)
else if (content is IDictionary dictionary && request.Method != HttpMethod.Get)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of if/else if going on here. Can this be refactored into a pattern-matched switch instead?

Copy link
Collaborator Author

@iSazonov iSazonov Feb 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purpose of this PR is to fix null checks. So code refactoring is out of the PR. Feel free to push new PR if you see benefits.

{
cellCount = _lo.DisplayCells.Length(fpf.propertyValue);
if (widths[kk] < cellCount)
widths[kk] = cellCount;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've seen of the codebase, it seems like the inline / two-line if statement is generally avoided in favor of the explicitly-braced form, even for simple if statements. Should this follow the existing patterns?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same.

{
cellCount = _lo.DisplayCells.Length(fpf.propertyValue);
if (cellCount > maxLen)
maxLen = cellCount;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vexx32 I consider such patterns as a "security" risk.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate @iSazonov?

Copy link
Collaborator Author

@iSazonov iSazonov Feb 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iSazonov iSazonov self-assigned this Feb 13, 2019
@iSazonov iSazonov merged commit ff3edaa into PowerShell:master Feb 13, 2019
@iSazonov iSazonov deleted the fix-lgtm-issues branch February 13, 2019 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants