dotnet / dotnet/sdk

CA2249 False positive

Open
#51,588 3 comments 0 reactions 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

**Version Used**: Visual Studio 2026 Insiders [11201.2]

**Steps to Reproduce**:

1. Paste this code:
```cs
string thing = "A bird was seen hopping.";
int indexOfSpace = thing.IndexOf(' ');
```

CA2249 is reported - three dots under the `thing.IndexOf(` line, suggesting to change that line into a `thing.Contains(`.

**Diagnostic Id**: CA2249

**Expected Behavior**:
The CA2249 warning - "replace IndexOf with string.Contains to improve readability", shouldn't be reported.

**Actual Behavior**:
It is reported, which doesn't make sense because we're trying to just get an index of a whitespace character in the string, not checking if it exists. Checking if it were to exist would've made sense if this was `thing.IndexOf(' ') != 0`, but it isn't, and the variable is of type `int`, so it's clear that we're just getting the index of a character than checking if the character exists.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.