Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S1117 FP: Field/property instances are not accessible from static methods #8260

Closed
cristian-ambrosini-sonarsource opened this issue Oct 26, 2023 · 3 comments · Fixed by #8937
Assignees
Labels
Area: C# C# rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Milestone

Comments

@cristian-ambrosini-sonarsource
Copy link
Contributor

The rule needs to distinguish between instance and static members.
Primary constructor parameters and instance fields/properties can only be shadowed in instance methods.

class Repro_XXXX(int primaryCtorParam)
{
    private int instanceField = 1;
    private int instanceProperty { get; set; } = 1;
    private static int staticField = 1;
    private static int staticProperty = 1;

    public static void StaticMethod()
    {
        var instanceField = 2; // Noncompliant FP (instance field is not accessible from static method)
        var instanceProperty = 2; // Noncompliant FP (instance property is not accessible from static method)
        var primaryCtorParam = 2; // Noncompliant FP (primary ctor parameter is not accessible from static method)

        var staticField = 2; // Noncompliant
        var staticProperty = 2; // Noncompliant
    }
}
@cristian-ambrosini-sonarsource
Copy link
Contributor Author

@martin-strecker-sonarsource: a SemanticModel.IsAccessible check is missing:
var fieldsAndProperties = members.Where(x => x is IPropertySymbol or IFieldSymbol).ToList();

Private fields/properties are accessible from within the declaring type but not derived types
protected/public fields/properties are accessible from the declaring and derived types.

See the original comment here.

@hankovich
Copy link

Any progress on this? Just got this warning after bumping package from 9.6.0.74858 to 9.21.0.86780

@cristian-ambrosini-sonarsource
Copy link
Contributor Author

Hi @hankovich, no progress on this yet.
We have a few hardening sprints in the next months and the issue has high priority, I think there's a good chance it will be tackled. Up until then, I suggest you mark it as FP.

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added this to the 9.23 milestone Mar 18, 2024
@github-actions github-actions bot added this to To do in Best Kanban Mar 18, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource moved this from To do to In progress in Best Kanban Mar 18, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added the Sprint: Hardening Fix FPs/FNs/improvements label Mar 18, 2024
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Mar 19, 2024
@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban Mar 19, 2024
Best Kanban automation moved this from Review approved to Validate Peach Mar 20, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource moved this from Validate Peach to Done in Best Kanban Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants