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 all the rest warnings raised by code analysis #3401

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
}