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 S2259 FP: Recognize NotNull results of String methods #6158

Closed
pavel-mikula-sonarsource opened this issue Oct 3, 2022 · 2 comments · Fixed by #7838
Closed

Fix S2259 FP: Recognize NotNull results of String methods #6158

pavel-mikula-sonarsource opened this issue Oct 3, 2022 · 2 comments · Fixed by #7838
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@pavel-mikula-sonarsource
Copy link
Contributor

pavel-mikula-sonarsource commented Oct 3, 2022

Methods like str.Trim() never return null. We should learn that from their invocations. It's similar to what we do for Linq Enumerable.

We need to go through the methods and identify those that never return null like Trim, SubString, and so on

It will fix this issue from Peach: https://peach.sonarsource.com/project/issues?issues=AYORbz8FkNA1ahe47Aev&open=AYORbz8FkNA1ahe47Aev&id=dotnet-runtime

        internal static Grammar Create(string grammarName, string ruleName)
        {
            grammarName = grammarName.Trim();
            Uri uriGrammar;
            bool hasUri = Uri.TryCreate(grammarName, UriKind.Absolute, out uriGrammar); // Is annotated with NotNullWhenAttribute, we learn Null and NotNull cases
            int posDll = grammarName.IndexOf(".dll", StringComparison.OrdinalIgnoreCase); // FP
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: Improvement Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Oct 3, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Improve S2259: Recognize NotNull results of String methods Fix S2259 FP: Recognize NotNull results of String methods Oct 3, 2022
@martin-strecker-sonarsource
Copy link
Contributor

According to https://raw.githubusercontent.com/dotnet/runtime/main/src/libraries/System.Runtime/ref/System.Runtime.cs there is only one method that may return null: string.IsInterned. It seems that whitelisting "IsInterned" is the easiest way for string.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: Improvement labels Aug 18, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 9.9 milestone Aug 18, 2023
@mary-georgiou-sonarsource
Copy link
Contributor

According to https://raw.githubusercontent.com/dotnet/runtime/main/src/libraries/System.Runtime/ref/System.Runtime.cs there is only one method that may return null: string.IsInterned. It seems that whitelisting "IsInterned" is the easiest way for string.

Maybe it's better to actually list the methods instead of just exclude one because in case a new String method is added that returns null we'll have an error.

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. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants