Skip to content

Commit

Permalink
fix all the rest warnings raised by code analysis (#3401)
Browse files Browse the repository at this point in the history
  • Loading branch information
heng-liu committed May 26, 2020
1 parent 9273710 commit 2ce3ec1
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,20 @@ public static string GetTargetPathForSourceFile(string sourcePath, string projec
}
var projectName = Path.GetFileName(projectDirectory);
var targetPath = Path.Combine(SourcesFolder, projectName);
#if NETCOREAPP
if (sourcePath.Contains(projectDirectory, StringComparison.Ordinal))
#else
if (sourcePath.Contains(projectDirectory))
#endif
{
// This is needed because Path.GetDirectoryName returns a path with Path.DirectorySepartorChar
var projectDirectoryWithSeparatorChar = PathUtility.GetPathWithDirectorySeparator(projectDirectory);

#if NETCOREAPP
var relativePath = Path.GetDirectoryName(sourcePath).Replace(projectDirectoryWithSeparatorChar, string.Empty, StringComparison.Ordinal);
#else
var relativePath = Path.GetDirectoryName(sourcePath).Replace(projectDirectoryWithSeparatorChar, string.Empty);
#endif
if (!string.IsNullOrEmpty(relativePath) && PathUtility.IsDirectorySeparatorChar(relativePath[0]))
{
relativePath = relativePath.Substring(1, relativePath.Length - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public static async Task<RestoreLogMessage> GetMessageAsync(string targetGraphNa
{
// Project
// Check if the name is a path and if it exists. All project paths should have been normalized and converted to full paths before this.
#if NETCOREAPP
if (unresolved.Name.IndexOf(Path.DirectorySeparatorChar, StringComparison.Ordinal) > -1 && File.Exists(unresolved.Name))
#else
if (unresolved.Name.IndexOf(Path.DirectorySeparatorChar) > -1 && File.Exists(unresolved.Name))
#endif
{
// File exists but the dg spec did not contain the spec
code = NuGetLogCode.NU1105;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,11 @@ private static HashSet<PackageDependencyInfo> ConvertToPackageDependencyInfo(

private static XElement GeneratePackagePathProperty(LocalPackageInfo localPackageInfo)
{
#if NETCOREAPP
return GenerateProperty($"Pkg{localPackageInfo.Id.Replace(".", "_", StringComparison.Ordinal)}", localPackageInfo.ExpandedPath);
#else
return GenerateProperty($"Pkg{localPackageInfo.Id.Replace(".", "_")}", localPackageInfo.ExpandedPath);
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,11 @@ private static void ClearIfExists<T>(IList<T> group) where T : LockFileItem
var fileName = Path.GetFileName(firstItem.Path);

Debug.Assert(!string.IsNullOrEmpty(fileName));
#if NETCOREAPP
Debug.Assert(firstItem.Path.IndexOf('/', StringComparison.Ordinal) > 0);
#else
Debug.Assert(firstItem.Path.IndexOf('/') > 0);
#endif

var emptyDir = firstItem.Path.Substring(0, firstItem.Path.Length - fileName.Length)
+ PackagingCoreConstants.EmptyFolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ private async Task<RepositorySignatureResource> GetRepositorySignatureResourceAs

private static string GenerateCacheKey(ServiceIndexEntry serviceEntry)
{
#if NETCOREAPP
var index = serviceEntry.Type.IndexOf('/', StringComparison.Ordinal);
#else
var index = serviceEntry.Type.IndexOf('/');
#endif
var version = serviceEntry.Type.Substring(index + 1).Trim();

return $"repository_signatures_{version}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ private static bool IsValidUriTemplate(string uriTemplate)
public Uri GetUri(string id, NuGetVersion version)
{
var uriString = _template
#if NETCOREAPP
.Replace("{id}", id, StringComparison.OrdinalIgnoreCase)
.Replace("{version}", version.ToNormalizedString(), StringComparison.OrdinalIgnoreCase);
#else
.Replace("{id}", id)
.Replace("{version}", version.ToNormalizedString());
#endif

return new Uri(uriString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,11 @@ private async Task<string> GetSecureApiKey(
}
catch(HttpRequestException ex)
{
#if NETCOREAPP
if (ex.Message.Contains("Response status code does not indicate success: 403 (Forbidden).", StringComparison.OrdinalIgnoreCase))
#else
if (ex.Message.Contains("Response status code does not indicate success: 403 (Forbidden)."))
#endif
{
return InvalidApiKey;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -33,8 +33,13 @@ public ReportAbuseResourceV3(string uriTemplate)
public Uri GetReportAbuseUrl(string id, NuGetVersion version)
{
var uriString = _uriTemplate
#if NETCOREAPP
.Replace("{id}", id, StringComparison.OrdinalIgnoreCase)
.Replace("{version}", version.ToNormalizedString(), StringComparison.OrdinalIgnoreCase);
#else
.Replace("{id}", id)
.Replace("{version}", version.ToNormalizedString());
#endif

return new Uri(uriString);
}
Expand Down
9 changes: 8 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,17 @@ public static string RemoveInvalidFileNameChars(string value)
{
var invalid = Path.GetInvalidFileNameChars();
return new string(
#if NETCOREAPP
value.Select(ch => invalid.Contains(ch) ? '_' : ch).ToArray()
)
.Replace("__", "_", StringComparison.Ordinal)
.Replace("__", "_", StringComparison.Ordinal);
#else
value.Select(ch => invalid.Contains(ch) ? '_' : ch).ToArray()
)
.Replace("__", "_")
.Replace("__", "_");
#endif
}
}
}
}
4 changes: 4 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/Utility/LocalFolderUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,11 @@ public static IEnumerable<string> ResolvePackageFromPath(string packagePath, boo

private static string EnsurePackageExtension(string packagePath, bool isSnupkg)
{
#if NETCOREAPP
if (packagePath.IndexOf('*', StringComparison.Ordinal) == -1)
#else
if (packagePath.IndexOf('*') == -1)
#endif
{
// If there's no wildcard in the path to begin with, assume that it's an absolute path.
return packagePath;
Expand Down
6 changes: 5 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/Utility/OfflineFeedUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public static void ThrowIfInvalid(string path)
}

var invalidPathChars = Path.GetInvalidPathChars();
#if NETCOREAPP
if (invalidPathChars.Any(p => path.Contains(p, StringComparison.Ordinal)))
#else
if (invalidPathChars.Any(p => path.Contains(p)))
#endif
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
Strings.Path_Invalid,
Expand Down Expand Up @@ -257,4 +261,4 @@ private static string GetHash(string nupkgFilePath)
return packageHash;
}
}
}
}

0 comments on commit 2ce3ec1

Please sign in to comment.