You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsoncode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is merged
Is the fixer's behavior of "whenever you see a byval argument, change it to byref" viable? That likely won't catch local copies being made within a method.
Method-call copies are probably almost always wrong. Local copies might be intentional, but if they're rare they can always suppress. (e.g. JsonDocument does a local copy to reset the ref if it hits an "I need more data" state)
I just ran my working prototype against dotnet/runtime and I found quite a few false positives in the System.Text.Json library. I found several false positives for the return by-value error in JsonElement.cs (EnumerateArray() and EnumerateObject()).
I also found several pass by-value false positives in ThrowHelper.Serialization.cs.
We may be able to improve the struct-enumerator heuristic to eliminate the return-by-value false positives. Currently, we recognize any nested value type that ends with "Enumerator" as a mutable value type, and we suppress diagnostics for methods named "GetEnumerator". We could also suppress methods containing "Enumerate", though I question whether that scenario is common enough to warrant special-casing. Might just be easier to suppress the warning in the runtime libraries.
Alternatively, we could simply not show a warning for method return values, since methods are likely to be factory methods. All of the return value false positives in dotnet/runtime were factory methods. I think properties should still be reported, though.
As for the pass by-value false positives, all of them are pass by-reference read-only parameters. I'm going to suggest that we not report diagnostics for "in" parameters. Since "in" is a language feature that beginners don't typically use, I think it's safe to assume that someone who marks a parameter as "in" likely knows what they're doing.
api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsoncode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is merged
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
carlossanlop commentedon Jan 15, 2021
@terrajobst is this what you intend the analyzer/fixer to do?:
Update: The answer below was "yes" :)
Suggested severity: Warning
Before
After
carlossanlop commentedon Feb 23, 2021
Ping @terrajobst , @GrabYourPitchforks and @bartonjs to help clarify our question above.
bartonjs commentedon Feb 23, 2021
Yep, the before/after is correct.
GrabYourPitchforks commentedon Feb 23, 2021
Is the fixer's behavior of "whenever you see a byval argument, change it to byref" viable? That likely won't catch local copies being made within a method.
bartonjs commentedon Feb 23, 2021
The fixer doesn't have to fix everything.
Method-call copies are probably almost always wrong. Local copies might be intentional, but if they're rare they can always suppress. (e.g. JsonDocument does a local copy to reset the ref if it hits an "I need more data" state)
GrabYourPitchforks commentedon Feb 23, 2021
Sounds reasonable. Carry on! :)
5 remaining items
NewellClark commentedon Aug 26, 2021
I would like to be assigned to this, please.
NewellClark commentedon Sep 6, 2021
I just ran my working prototype against dotnet/runtime and I found quite a few false positives in the
System.Text.Jsonlibrary. I found several false positives for the return by-value error in JsonElement.cs (EnumerateArray()andEnumerateObject()).I also found several pass by-value false positives in ThrowHelper.Serialization.cs.
We may be able to improve the struct-enumerator heuristic to eliminate the return-by-value false positives. Currently, we recognize any nested value type that ends with "Enumerator" as a mutable value type, and we suppress diagnostics for methods named "GetEnumerator". We could also suppress methods containing "Enumerate", though I question whether that scenario is common enough to warrant special-casing. Might just be easier to suppress the warning in the runtime libraries.
Alternatively, we could simply not show a warning for method return values, since methods are likely to be factory methods. All of the return value false positives in dotnet/runtime were factory methods. I think properties should still be reported, though.
As for the pass by-value false positives, all of them are pass by-reference read-only parameters. I'm going to suggest that we not report diagnostics for "in" parameters. Since "in" is a language feature that beginners don't typically use, I think it's safe to assume that someone who marks a parameter as "in" likely knows what they're doing.
buyaa-n commentedon Aug 14, 2023
Moving to future milestone as the PR blocked with an open question