Skip to content

Fix some "nullable reference type" warnings#26

Open
rmunn wants to merge 6 commits into
mainfrom
chore/fix-some-nullable-warnings
Open

Fix some "nullable reference type" warnings#26
rmunn wants to merge 6 commits into
mainfrom
chore/fix-some-nullable-warnings

Conversation

@rmunn
Copy link
Copy Markdown
Collaborator

@rmunn rmunn commented Jun 21, 2024

This fixes some of the compiler warnings about nullable reference types that you get when you build this with a modern C# compiler. It doesn't fix nearly all of them, because I realized that a lot of these can't be easily fixed as long as the project is targeting net461. For example, this section of code produces a compiler warning on line 276 that switchToCitationForm might be null:

else if(!string.IsNullOrEmpty(switchToCitationForm))
{
//ok, now we're in the FLEx 5.4 situation where it
//it's not going to link to the \lx because there is a different
// \lc in there (only matches to the headword, which is
//lx unless there is an lc. So now we switch the referrer to the lc.
addition.sourceField.Value = switchToCitationForm;

It's immediately obvious that switchToCitationForm cannot be null on line 276, but the compiler doesn't know that as long as the target framework is net461. That's because the annotations that tell the compiler "If this function returns true/false, then its parameter can/cannot be null" were added to library code sometime during the .NET Core / .NET 5 era, and the net461 framework libraries don't have those annotations. So the compiler can't infer anything about the nullness of the parameter to string.IsNullOrEmpty, and therefore it gives a warning. You could suppress it by using the ! operator, as I did in this particular case, but doing that everywhere is tedious. And ultimately pointless, when switching to a more modern .NET target will end up fixing the issue anyway.

Therefore, I suggest postponing the rest of this work until after the code is switched to target a more modern version of .NET, say 6 or 8. At that point many of the compiler warnings will go away, and the ones that are left will be ones actually worth fixing.

rmunn added 5 commits June 21, 2024 09:54
This only works in .NET 5.0 or later, though, so for net461 we'll need a
different approach.
The pattern where the constructor calls an Initialize() method is one
that the C# compiler can't analyze to detect that nullable reference
types are actually assigned a real value. So we move most of the code
into the constructor (and the static fields are initializes in a static
constructor) so that the compiler will stop warning us about them.

This still leaves a few compiler warnings that are real, such as the one
about _realDictionaryPath, which could indeed be accessed before it is
initialized. That one, and a few others, should truly be typed nullable.
Not all nullable warnings will be able to be removed as long as this
code still targets net461; for example, string.IsNullOrEmpty guarantees
that the string isn't null, but the annotations required for the
compiler to know that weren't added until the .NET Core era. So until
the projects targets a modern version of .NET, the only way to get rid
of nullable references like these is to use the ! operator every time,
which will quickly get tedious.
@megahirt
Copy link
Copy Markdown
Contributor

After some discussion in the office, we will hold off on merging this until we can understand more about the effort it will take to upgrade to .Net 8 windows forms. Upgrading will eliminate some compiler warnings and reveal the ones that are actually a problem.

@megahirt
Copy link
Copy Markdown
Contributor

@rmunn would you say that this work shouldn't be merged, and we should abandon this PR in favor up a full upgrade to .Net 8? If so, I'm happy to close the PR without merging. Thanks for your investigation of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants