Skip to content

Commit 5cdafbc

Browse files
authored
Enable CA1858: Use 'StartsWith' instead of 'IndexOf' (#26107)
1 parent 0ef738b commit 5cdafbc

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

.globalconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ dotnet_diagnostic.CA1846.severity = warning
510510
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1847
511511
dotnet_diagnostic.CA1847.severity = warning
512512

513+
# CA1858: Use 'StartsWith' instead of 'IndexOf'
514+
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1858
515+
dotnet_diagnostic.CA1858.severity = warning
516+
513517
# CA1868: Unnecessary call to 'Contains' for sets
514518
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1868
515519
dotnet_diagnostic.CA1868.severity = warning

src/Microsoft.PowerShell.Security/security/SecureStringCommands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ protected override void ProcessRecord()
311311
byte[] iv = null;
312312

313313
// If this is a V2 package
314-
if (String.IndexOf(SecureStringHelper.SecureStringExportHeader,
315-
StringComparison.OrdinalIgnoreCase) == 0)
314+
if (String.StartsWith(SecureStringHelper.SecureStringExportHeader, StringComparison.OrdinalIgnoreCase))
316315
{
317316
try
318317
{

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8286,7 +8286,7 @@ internal static void DeleteFileStream(string path, string streamName)
82868286
ArgumentNullException.ThrowIfNull(streamName);
82878287

82888288
string adjustedStreamName = streamName.Trim();
8289-
if (adjustedStreamName.IndexOf(':') != 0)
8289+
if (!adjustedStreamName.StartsWith(':'))
82908290
{
82918291
adjustedStreamName = ":" + adjustedStreamName;
82928292
}

src/System.Management.Automation/namespaces/LocationGlobber.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4539,7 +4539,7 @@ internal static bool IsHomePath(string path)
45394539
}
45404540
}
45414541

4542-
if (path.IndexOf(StringLiterals.HomePath, StringComparison.Ordinal) == 0)
4542+
if (path.StartsWith(StringLiterals.HomePath, StringComparison.Ordinal))
45434543
{
45444544
// Support the single "~"
45454545
if (path.Length == 1)
@@ -4638,7 +4638,7 @@ internal string GetHomeRelativePath(string path)
46384638
}
46394639
}
46404640

4641-
if (path.IndexOf(StringLiterals.HomePath, StringComparison.Ordinal) == 0)
4641+
if (path.StartsWith(StringLiterals.HomePath, StringComparison.Ordinal))
46424642
{
46434643
// Strip of the ~ and the \ or / if present
46444644

src/System.Management.Automation/security/Authenticode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ internal static Signature SignFile(SigningOption option,
115115
if (!string.IsNullOrEmpty(timeStampServerUrl))
116116
{
117117
if ((timeStampServerUrl.Length <= 7) || (
118-
(timeStampServerUrl.IndexOf("http://", StringComparison.OrdinalIgnoreCase) != 0) &&
119-
(timeStampServerUrl.IndexOf("https://", StringComparison.OrdinalIgnoreCase) != 0)))
118+
!timeStampServerUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
119+
!timeStampServerUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
120120
{
121121
throw PSTraceSource.NewArgumentException(
122122
nameof(certificate),

0 commit comments

Comments
 (0)