Skip to content

CA2249 False positive #51588

@winscripter

Description

@winscripter

Version Used: Visual Studio 2026 Insiders [11201.2]

Steps to Reproduce:

  1. Paste this code:
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions