Skip to content

Enable nullable reference type warnings#207

Open
FantasyTeddy wants to merge 1 commit into
sharpbrick:masterfrom
FantasyTeddy:nullable-warnings
Open

Enable nullable reference type warnings#207
FantasyTeddy wants to merge 1 commit into
sharpbrick:masterfrom
FantasyTeddy:nullable-warnings

Conversation

@FantasyTeddy

@FantasyTeddy FantasyTeddy commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

This could be a first step towards enabling nullable reference types.

#205 non-breaking

@tthiery

tthiery commented Jun 6, 2026

Copy link
Copy Markdown
Member

Hi @FantasyTeddy ... thanks for your PR. I have not run a build so far. Are these all the nullable errors?

Can we quickly outline what your plan is ;)

//we only want to discover Lego-devices
//filter for correct service-GUID for LEGO-Hub, containing manufacturerData and the name
var addressNumber = BlueGigaBLEHelper.ByteArrayToUlong(e.Address);
var bleAdvertisingListFound = bleParsedData.TryGetValue(addressNumber, out var actualListAdvertisingData);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this change necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the additional variable, the compiler can no longer correctly infer that actualListAdvertisingData cannot be null if TryGetValue returns true.

var dumpStaticPortInfoCommand = scopedServiceProvider.GetRequiredService<DumpStaticPortInfo>(); // ServiceLocator ok: transient factory

var port = byte.Parse(portOption.Value());
var port = byte.TryParse(portOption.Value(), out var parsedPort) ? parsedPort : (byte)0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not that a behavior change to the CLI?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess technically, yes. But wasn't the behavior before that omitting the -p port option would lead to an exception here?

public Hub CreateByBluetoothManufacturerData(byte[] manufacturerData)
{
var hub = (manufacturerData is null || manufacturerData.Length < 3) ? null : Create(GetTypeFromSystemType(GetSystemTypeFromManufacturerData((PoweredUpHubManufacturerData)manufacturerData[1])));
if (manufacturerData is null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is right change. Maybe the function needs a Hub? as a result. Obviously the init line needs to change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried to keep the changes minimal.
Before this change, the call to Configure later would throw a NullReferenceException anyway, and I figured that throwing a more meaningful exception would be better.

@FantasyTeddy

Copy link
Copy Markdown
Contributor Author

Can we quickly outline what your plan is ;)

Sure, sorry for dumping a bunch of PRs without any real explanation.

I would love to enable nullable reference types in this project to catch some potential bugs and also help users to better understand the contract behind some of the code.
However, I was not yet able to enable it fully (<Nullable>enable</Nullable>) because there are a lot of things that require deeper understanding of the library.
Enabling warnings first is another migration strategy that can at least do a first step towards that goal.

@tthiery

tthiery commented Jun 6, 2026

Copy link
Copy Markdown
Member

Nullable is a good path. Let us do this. But we should finish it before the 6.0 release then. I do not want hundreds of warnings in the build.

@FantasyTeddy

Copy link
Copy Markdown
Contributor Author

I would love that, but there are currently no warnings from that first step, because I tried to fix them.
Additional warnings would only appear if we fully enabled nullable reference types (<Nullable>enable</Nullable>).

@tthiery

tthiery commented Jun 6, 2026

Copy link
Copy Markdown
Member

How bad is it? Or do you need me to take a shot at it?

@FantasyTeddy

Copy link
Copy Markdown
Contributor Author

There are quite a lot if we enable it fully. I had to give up, due to a lack of understanding of the functionality.

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