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

Address build warnings from .NET 8 analyzers #764

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/LibraryManager.Contracts/CompletionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@ public bool Equals(CompletionItem other)
/// </summary>
public override int GetHashCode()
{
#if NET8_0_OR_GREATER
return HashCode.Combine(InsertionText, Description);
#else
// Much of the time, InsertionText == DisplayText, so XORing those would just cancel out.
// So don't include DisplayText in the hash code.
return InsertionText.GetHashCode() ^ Description.GetHashCode();
#endif
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/LibraryManager/Cache/CacheFileMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public override bool Equals(object obj)
/// <inheritdoc />
public override int GetHashCode()
{
#if NET8_0_OR_GREATER
return DestinationPath.GetHashCode(StringComparison.Ordinal); // this should be a unique identifier
#else
return DestinationPath.GetHashCode(); // this should be a unique identifier
#endif
}
}
}
5 changes: 4 additions & 1 deletion src/LibraryManager/FileIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

namespace Microsoft.Web.LibraryManager
{
Expand Down Expand Up @@ -35,10 +34,14 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
#if NET8_0_OR_GREATER
return HashCode.Combine(Path, Version);
#else
int hashPath = Path == null ? 0 : Path.GetHashCode();
int hashVersion = Version == null ? 0 : Version.GetHashCode();

return hashPath ^ hashVersion;
#endif
}
}
}
6 changes: 6 additions & 0 deletions src/LibraryManager/Minimatch/Minimatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,13 @@ private bool Match(string input, bool partial)
// On other platforms, \ is a valid (albeit bad) filename char.

if (options.AllowWindowsPaths)
{
#if NET8_0_OR_GREATER
input = input.Replace("\\", "/", StringComparison.Ordinal);
#else
input = input.Replace("\\", "/");
#endif
}

// treat the test path as a set of pathparts.
var f = slashSplit.Split(input);
Expand Down
3 changes: 2 additions & 1 deletion src/LibraryManager/Providers/BaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ protected virtual string GetCachedFileLocalPath(ILibraryInstallationState state,
/// <summary>
/// Copies ILibraryInstallationState files to cache
/// </summary>
/// <param name="state"></param>
/// <param name="state">Desired install state to cache</param>
/// <param name="library">Library resolved from provider</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<ILibraryOperationResult> RefreshCacheAsync(ILibraryInstallationState state, ILibrary library, CancellationToken cancellationToken)
Expand Down
4 changes: 4 additions & 0 deletions src/LibraryManager/Providers/Cdnjs/CdnjsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public async Task<CompletionSet> GetLibraryCompletionSetAsync(string value, int
Length = value.Length
};

#if NET8_0_OR_GREATER
int at = value.IndexOf('@', StringComparison.Ordinal);
#else
int at = value.IndexOf('@');
#endif
string name = at > -1 ? value.Substring(0, at) : value;

var completions = new List<CompletionItem>();
Expand Down
3 changes: 3 additions & 0 deletions src/LibraryManager/Providers/FileSystem/FileSystemCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -37,6 +38,7 @@ public FileSystemCatalog(FileSystemProvider provider, bool underTest = false)
/// <param name="value">The current state of the library ID.</param>
/// <param name="caretPosition">The caret position inside the <paramref name="value" />.</param>
/// <returns></returns>
[SuppressMessage("Globalization", "CA1307:Specify StringComparison for clarity", Justification = "Searching for characters which are not cased")]
public Task<CompletionSet> GetLibraryCompletionSetAsync(string value, int caretPosition)
{
if (value.Contains("://"))
Expand Down Expand Up @@ -122,6 +124,7 @@ public Task<CompletionSet> GetLibraryCompletionSetAsync(string value, int caretP
/// <returns>
/// An instance of <see cref="Microsoft.Web.LibraryManager.Contracts.ILibraryGroup" /> or <code>null</code>.
/// </returns>
[SuppressMessage("Globalization", "CA1307:Specify StringComparison for clarity", Justification = "Searching for characters which are not cased")]
public async Task<ILibrary> GetLibraryAsync(string libraryName, string version, CancellationToken cancellationToken)
{
ILibrary library;
Expand Down
2 changes: 2 additions & 0 deletions src/LibraryManager/Providers/jsDelivr/JsDelivrCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -343,6 +344,7 @@ private async Task<IEnumerable<string>> GetGithubLibraryVersionsAsync(string nam
return versions;
}

[SuppressMessage("Globalization", "CA1307:Specify StringComparison for clarity", Justification = "Searching for characters which are not cased")]
public static bool IsGitHub(string libraryId)
{
if (libraryId == null || libraryId.StartsWith("@", StringComparison.Ordinal))
Expand Down
5 changes: 5 additions & 0 deletions src/LibraryManager/RelativePathEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;

Expand Down Expand Up @@ -37,7 +38,11 @@ public bool Equals(string x, string y)
/// <inheritdoc />
public int GetHashCode(string obj)
{
#if NET8_0_OR_GREATER
return NormalizePath(obj)?.GetHashCode(StringComparison.Ordinal) ?? 0;
#else
return NormalizePath(obj)?.GetHashCode() ?? 0;
#endif
}

private string NormalizePath(string path)
Expand Down
11 changes: 10 additions & 1 deletion src/LibraryManager/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public class SemanticVersion : IComparable<SemanticVersion>, IEquatable<Semantic

private SemanticVersion(string originalText)
{
#if NET8_0_OR_GREATER
_hashCode = originalText?.GetHashCode(StringComparison.Ordinal) ?? 0;
#else
_hashCode = originalText?.GetHashCode() ?? 0;
#endif
OriginalText = originalText;
}

Expand All @@ -57,8 +61,13 @@ internal static SemanticVersion Parse(string value)
return ver;
}

#if NET8_0_OR_GREATER
int prereleaseStart = value.IndexOf('-', StringComparison.Ordinal);
int buildMetadataStart = value.IndexOf('+', StringComparison.Ordinal);
#else
int prereleaseStart = value.IndexOf('-');
int buildMetadataStart = value.IndexOf('+');
#endif

//If the index of the build metadata marker (+) is greater than the index of the prerelease marker (-)
// then it is necessarily found in the string because if both were not found they'd be equal
Expand Down Expand Up @@ -197,7 +206,7 @@ public int CompareTo(SemanticVersion other)
}

/// <summary>
/// Returns whether the semantic verisons are equal. This includes comparing the build metadata, and does not provide semantic equivalence.
/// Returns whether the semantic versions are equal. This includes comparing the build metadata, and does not provide semantic equivalence.
/// </summary>
public bool Equals(SemanticVersion other)
{
Expand Down