Skip to content

Enable CA1858: Use 'StartsWith' instead of 'IndexOf'#26107

Merged
iSazonov merged 1 commit into
PowerShell:masterfrom
xtqqczze:CA1858
Oct 8, 2025
Merged

Enable CA1858: Use 'StartsWith' instead of 'IndexOf'#26107
iSazonov merged 1 commit into
PowerShell:masterfrom
xtqqczze:CA1858

Conversation

@xtqqczze
Copy link
Copy Markdown
Contributor

@xtqqczze xtqqczze commented Sep 28, 2025

It's more efficient and clearer to call String.StartsWith than to call String.IndexOf and compare the result with zero to determine whether a string starts with a given prefix.
IndexOf searches the entire string, while StartsWith only compares at the beginning of the string.

https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1858

@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Sep 29, 2025

Error: D:\a\PowerShell\PowerShell\src\System.Management.Automation\namespaces\FileSystemProvider.cs(8289,17): error CA1858: Use 'StartsWith' instead of comparing the result of 'IndexOf' to 0 (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1858) [D:\a\PowerShell\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]

Learn about code analyzer rule CA1858 - Use StartsWith instead of IndexOf

@xtqqczze
Copy link
Copy Markdown
Contributor Author

xtqqczze commented Oct 6, 2025

Error: D:\a\PowerShell\PowerShell\src\System.Management.Automation\namespaces\FileSystemProvider.cs(8289,17)

fixed in efc14b8

@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Oct 7, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 7, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@iSazonov iSazonov closed this Oct 7, 2025
@iSazonov iSazonov reopened this Oct 7, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 7, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@iSazonov iSazonov self-assigned this Oct 8, 2025
@iSazonov iSazonov merged commit 5cdafbc into PowerShell:master Oct 8, 2025
64 of 67 checks passed
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

microsoft-github-policy-service Bot commented Oct 8, 2025

📣 Hey @@xtqqczze, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@xtqqczze xtqqczze deleted the CA1858 branch October 8, 2025 03:27
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
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.

2 participants