-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
v13 vs. v14: C# client generates a lot more warnings in v14 than in v13. #4631
Comments
@paulomorgado are these regressions of your latest changes? _baseUrl has been changed here: can you fix this? |
Do we have a unit test for this? If not, where/how can it be added? |
Should we be concerned about these warnings in the generated code? It's fine to just get rid of the warning with the null forgiving operator. But only if we are sure that it won't be |
v13 always initialized As for the concern for warnings, strictly speaking there is no concern since "they are only warnings". |
You should never ignore warnings as they are hinting to possible errors. You have 3 options:
|
Ignoring errors in non-generated code is always my advise. None of these options are viable options.
|
That field is not directly accessed anymore and its value is initialized in the constructor via the I understand why you are running into an issue here and I can relate to it, although generated code should be checked for in the context of the generator and not the generated code. In fact, like you just did by reporting it here and not by checking generated code. I'll add a |
Added #pragma warning disable 8618 // Set by constructor via BaseUrl property
private string _baseUrl;
#pragma restore disable 8618 // Set by constructor via BaseUrl property to PR #4634. |
I'm always concerned about warnings, because they hint to possible run-time issues. It's an illusion to think one can enforce whatever policies on code you don't control. If this was a library or a tool generating IL, that would not be source checked. Or would it be? What would be the way of "writing the code correctly" in this case? |
This does not solve the warning because the warning is raised on the constructor, not on the property. Additionally this prevents users from suppressing this warning (and others) using a custom file header template as well. In general it is better to suppress such warnings using the null forgiving operator rather than supressing. This code does the same trick and is less verbose. private string _baseUrl = null!; |
@klemmchr, can you provide practical examples of this? |
We are setting |
After upgrading our .NET 6 project from v13 to v14, we notice two things:
(1) Issue #4467 is still valid. The warnings:
... are still issued because the methods
WriteJson
andReadJson
in `Newtonsoft.Json.JsonConverter are defined as:... whereas in the generated code, the methods are overriden with the following signature:
(2) All client types generate the following warning in their constructor:
This is because the field
_baseUrl
was defined as follows in v13:... whereas in v14, it's just:
But since the constructor doesn't reference it, a warning is issued since it's a non-nullable reference.
The removal of the initialization string was a good move since it was confusing. To get rid of the error, one can use a "null forgiving operator":
This will get rid of the warning.
The text was updated successfully, but these errors were encountered: