-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Do not pass Utf8JsonReader by value #33772
Do not pass Utf8JsonReader by value #33772
Comments
Estimates:
|
@terrajobst is this what you intend the analyzer/fixer to do?: Suggested severity: Warning Beforestatic void Main()
{
Utf8JsonReader reader = new(/**/);
MyMethod(reader);
}
static void MyMethod(Utf8JsonReader reader)
{
} Afterstatic void Main()
{
Utf8JsonReader reader = new(/**/);
MyMethod(ref reader);
}
static void MyMethod(ref Utf8JsonReader reader)
{
} |
Ping @terrajobst , @GrabYourPitchforks and @bartonjs to help clarify our question above. |
Yep, the before/after is correct. |
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. |
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) |
Sounds reasonable. Carry on! :) |
Thank you, both! I'll mark this as ready for review. |
Severity: Warning
|
I would like to be assigned to this, please. |
I just ran my working prototype against dotnet/runtime and I found quite a few false positives in the 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. 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. |
Moving to future milestone as the PR blocked with an open question |
The rule should flag cases where a reader is passed around by value.
Category: Reliability
The text was updated successfully, but these errors were encountered: