Skip to content

IDE0370 reports field! in a property getter as unnecessary, but removing it causes CS9264 #86573

Open

Description

@Manish-1kumar

Version Used: .NET SDK 10.0.401 (compiler and CodeStyle 5.9.0-1.26423.113); the analyzer is unchanged on main.

Steps to Reproduce:

#nullable enable

public class A
{
    public string Name { get => field!; set => field = value; }
}

public class B
{
    public B() { }
    public string Name { get { return field!; } set; }
}

public class C
{
    public C() { }
    public string Name => field!;
}
  1. Build with dotnet_diagnostic.IDE0370.severity = warning: the only diagnostics are IDE0370: Suppression is unnecessary on each field!.
  2. Apply the fix (dotnet format style --diagnostics IDE0370). Each field! becomes field.
  3. Build again.

Expected Behavior: IDE0370 isn't reported, because the ! is what makes the getter null-resilient and suppresses CS9264.

Actual Behavior: after the fix the build gets three new warnings, CS9264: Non-nullable property 'Name' must contain a non-null value when exiting constructor..., on the property in A and on the constructors in B and C. With TreatWarningsAsErrors the build fails.

Cause: the field-keyword proposal says null-forgiving operators are respected when deciding whether the getter is null-resilient. UnnecessaryNullableWarningSuppressionsUtilities re-checks the code without the !, but only keeps diagnostics that intersect the containing member: the accessor for get => field! / get { return field!; }, or the property for string Name => field! (whose diagnostics are computed for the whole type but then filtered to the property's span). CS9264 is reported on the property name or on the constructor, outside those spans, so it's never seen. There are no field keyword cases in the existing tests. #81843 is a different IDE0370 false positive.

Possible fix: when the suppression is in the getter of a property that uses field, check diagnostics across the containing type (including other partial declarations), or don't offer the removal in that case, plus tests. Happy to send a PR if you agree.

Activity

  1. CyrusNajmabadi commented on Nov 22, 2026

    @CyrusNajmabadi
    Contributor

    yes. we should include the containing property name-span, and the constructor name-span if those are the locations these warnings get emitted.

  2. Manish-1kumar commented on Nov 22, 2026

    @Manish-1kumar
    Author

    Thanks, I'll send a PR along those lines: when the suppression is in the getter of a property that uses field, also check the diagnostics at the property's identifier and at the containing type's constructor identifiers. If a constructor is in another partial declaration (another file), I'll just not offer the removal there.

  3. CyrusNajmabadi commented on Nov 22, 2026

    @CyrusNajmabadi
    Contributor

    . If a constructor is in another partial declaration (another file), I'll just not offer the removal there.

    we can analyze contructors in other parts in other files. it's fine to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions