Skip to content
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

Turn on C# 8's null checker #458

Open
10 of 14 tasks
dahlia opened this issue Aug 26, 2019 · 18 comments
Open
10 of 14 tasks

Turn on C# 8's null checker #458

dahlia opened this issue Aug 26, 2019 · 18 comments
Assignees
Labels
help wanted Extra attention is needed suggestion Suggestion or feature request

Comments

@dahlia
Copy link
Contributor

dahlia commented Aug 26, 2019

Edit: There is a way to introduce C# 8 with still targeting .NET Standard 2.0. For several reasons, we decided to use C# 8, but at the moment we use it without <Nullable>enable</Nullable> option. I hope we turn it on as well in the near future!

Although C# 8 will have non-nullable reference types, it will requires .NET Standard 2.1 (note that we currently depend on .NET Standard 2.0), which is unlikely to supported by Unity in the near future.

Instead, I suggest to introduce JetBrain's NotNullAttribute until Unity becomes to support .NET Standard 2.1. There's a Roslyn Analyzer implementation named NullGuard.Fody which does null-checks according to NotNullAttribute at compile time.


Projects (test projects are excluded as of now):

  • Libplanet
    • Enabled by default
    • Enabled 205/256 files (80%)
  • Libplanet.Analyzers
    • Enabled by default
    • Enabled all files (100%)
  • Libplanet.Explorer
    • Enabled by default
    • Enabled 5/24 files (20%)
  • Libplanet.Extensions.Cocona
    • Enabled by default
    • Enabled all files (100%)
  • Libplanet.RocksDBStore
    • Enabled by default
    • Enabled 5/6 files (84%)
  • Libplanet.Stun
    • Enabled by default
    • Enabled 11/45 files (24%)
  • Libplanet.Tools
    • Enabled by default
    • Enabled all files (100%)
@dahlia dahlia added the suggestion Suggestion or feature request label Aug 26, 2019
@stale
Copy link

stale bot commented Oct 30, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale The issue is stale label Oct 30, 2019
@stale stale bot closed this as completed Nov 6, 2019
@limebell limebell reopened this Nov 7, 2019
@stale stale bot removed the stale The issue is stale label Nov 7, 2019
@stale
Copy link

stale bot commented Jan 6, 2020

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot added the stale The issue is stale label Jan 6, 2020
@dahlia
Copy link
Contributor Author

dahlia commented Jan 29, 2020

If we decide to use C# 8.0 with .NET Standard 2.0 (see #780) this approach is probably not that useful.

@stale stale bot removed the stale The issue is stale label Jan 29, 2020
@dahlia
Copy link
Contributor Author

dahlia commented Feb 12, 2020

If we decide to use C# 8.0 with .NET Standard 2.0 (see #780) this approach is probably not that useful.

We decided to use C# 8.0 with .NET Standard 2.0, not for null checks, but async streams. Anyway we would be able to use null checks as well, if we introduce C# 8.0 soon.

@dahlia dahlia changed the title Introduce NotNullAttribute and NullGuard.Fody Turn on C# 8's null checker Feb 12, 2020
@dahlia dahlia added the help wanted Extra attention is needed label Mar 11, 2020
@stale
Copy link

stale bot commented May 10, 2020

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot added the stale The issue is stale label May 10, 2020
@dahlia dahlia removed the stale The issue is stale label Aug 6, 2020
@dahlia
Copy link
Contributor Author

dahlia commented Aug 6, 2020

Update: although we still haven't enabled null checker project-wide, we've already enabled it in several files (the recently added files for the most part). For gradual progression, I suggest the @planetarium/libplanet team the methodology to adopt null checker with the minimum pain (and actually it is what we've recently done in few months indeed):

  • When we add a new file, it should enable null checker in that file. E.g.:

    #nullable enable

  • When we change a file of not-that-long lines, let's try to enable null checker in that file. However, if it seems going deeper, retreat and wait a chance for next time.

  • If we feel more than 2/3 in the code base (count only Libplanet project) let's try to enable null checker project-wide.

@libplanet Opinions?

@dahlia
Copy link
Contributor Author

dahlia commented Sep 22, 2020

The current status:

$ find Libplanet/ -name '*.cs' | wc -l
193
$ git grep '#nullable enable' Libplanet/ | wc -l
37

The easiest files to enable null checker would be exception classes:

$ for f in $(find Libplanet -name '*Exception.cs'); do grep '#nullable enable' "$f" > /dev/null || echo "$f"; done
Libplanet/Blocks/InvalidBlockNonceException.cs
Libplanet/Blocks/InvalidBlockDifficultyException.cs
Libplanet/Blocks/InvalidBlockPreviousHashException.cs
Libplanet/Blocks/InvalidBlockTimestampException.cs
Libplanet/Blocks/InvalidBlockIndexException.cs
Libplanet/Blocks/InvalidGenesisBlockException.cs
Libplanet/Blocks/InvalidBlockHashException.cs
Libplanet/Blocks/InvalidBlockException.cs
Libplanet/Crypto/InvalidCiphertextException.cs
Libplanet/Net/SwarmException.cs
Libplanet/Net/DifferentAppProtocolVersionException.cs
Libplanet/Net/InvalidStateTargetException.cs
Libplanet/Net/InvalidMessageException.cs
Libplanet/Net/IceServerException.cs
Libplanet/Net/PeerNotFoundException.cs
Libplanet/Net/NoSwarmContextException.cs
Libplanet/Net/Protocols/PingTimeoutException.cs
Libplanet/Net/Protocols/PeerDiscoveryException.cs
Libplanet/KeyStore/IncorrectPassphraseException.cs
Libplanet/KeyStore/InvalidKeyJsonException.cs
Libplanet/KeyStore/KeyJsonException.cs
Libplanet/KeyStore/UnsupportedKeyJsonException.cs
Libplanet/KeyStore/NoKeyException.cs
Libplanet/KeyStore/KeyStoreException.cs
Libplanet/KeyStore/MismatchedAddressException.cs
Libplanet/Tx/InvalidTxNonceException.cs
Libplanet/Tx/InvalidTxException.cs
Libplanet/Tx/InvalidTxGenesisHashException.cs
Libplanet/Tx/InvalidTxPublicKeyException.cs
Libplanet/Tx/InvalidTxSignatureException.cs
Libplanet/Tx/InvalidTxUpdatedAddressesException.cs
Libplanet/Tx/TxViolatingBlockPolicyException.cs
Libplanet/Tx/InvalidTxIdException.cs
Libplanet/Blockchain/IncompleteBlockStatesException.cs
Libplanet/Action/MissingActionTypeException.cs
Libplanet/Action/UnexpectedlyTerminatedActionException.cs
Libplanet/Store/ChainIdNotFoundException.cs

@stale
Copy link

stale bot commented Nov 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot added the stale The issue is stale label Nov 21, 2020
@stale stale bot removed the stale The issue is stale label Apr 26, 2021
@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot added the stale The issue is stale label Jun 26, 2021
@dahlia
Copy link
Contributor Author

dahlia commented Jan 18, 2022

The current status:

$ find Libplanet/ -name '*.cs' | wc -l
248
$ git grep '#nullable enable' Libplanet/ | wc -l
197

Progress: 79.4%.

Now we have more null-checked files than unchecked files, we can enable it project-wide (in Libplanet/Libplanet.csproj file), and opt out only exceptional files.

@stale stale bot removed the stale The issue is stale label Jan 18, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Jan 24, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Jan 24, 2022
As null checker is globally turned on in Libplanet project,
each single file in the project no more needs to
add a #null enable directive

planetarium#458
dahlia added a commit that referenced this issue Jan 24, 2022
As null checker is globally turned on in Libplanet project,
each single file in the project no more needs to
add a #null enable directive

#458
dahlia added a commit to dahlia/libplanet that referenced this issue Jan 25, 2022
As null checker is globally turned on in Libplanet project,
each single file in the project no more needs to
add a #null enable directive

planetarium#458
dahlia added a commit to dahlia/libplanet that referenced this issue Jan 25, 2022
As null checker is globally turned on in Libplanet project,
each single file in the project no more needs to
add a #null enable directive.

planetarium#458
dahlia added a commit to dahlia/libplanet that referenced this issue Jan 25, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Jan 25, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Jan 25, 2022
@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot added the stale The issue is stale label Apr 16, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Jul 29, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Jul 29, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Aug 2, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Aug 3, 2022
dahlia added a commit to dahlia/libplanet that referenced this issue Aug 5, 2022
OnedgeLee pushed a commit to OnedgeLee/libplanet that referenced this issue Aug 9, 2022
@s2quake s2quake self-assigned this Jan 17, 2024
@stale stale bot removed the stale The issue is stale label Jan 17, 2024
@riemannulus
Copy link
Member

@s2quake got this.

@s2quake
Copy link
Contributor

s2quake commented Jan 17, 2024

The status of the entire solution before starting the task is as follows.

Libplanet.Stun/Libplanet.Stun.csproj
    cs      : 44
    nullable: 34
    progress: 77.27%
Libplanet/Libplanet.csproj
    cs      : 54
    nullable: 13
    progress: 24.07%
Libplanet.RocksDBStore/Libplanet.RocksDBStore.csproj
    cs      : 6
    nullable: 1
    progress: 16.67%
Libplanet.Tools/Libplanet.Tools.csproj
    cs      : 2
    nullable: 0
    progress: 0.00%
Libplanet.Analyzers/Libplanet.Analyzers.csproj
    cs      : 4
    nullable: 0
    progress: 0.00%
Libplanet.Explorer/Libplanet.Explorer.csproj
    cs      : 43
    nullable: 8
    progress: 18.60%
Libplanet.Extensions.Cocona/Libplanet.Extensions.Cocona.csproj
    cs      : 19
    nullable: 0
    progress: 0.00%
Libplanet.Extensions.Cocona.Tests/Libplanet.Extensions.Cocona.Tests.csproj
    cs      : 9
    nullable: 2
    progress: 22.22%
Libplanet.Net/Libplanet.Net.csproj
    cs      : 112
    nullable: 10
    progress: 8.93%
Libplanet.Net.Tests/Libplanet.Net.Tests.csproj
    cs      : 50
    nullable: 17
    progress: 34.00%
Libplanet.Crypto.Secp256k1/Libplanet.Crypto.Secp256k1.csproj
    cs      : 3
    nullable: 0
    progress: 0.00%
Libplanet.Crypto.Secp256k1.Tests/Libplanet.Crypto.Secp256k1.Tests.csproj
    cs      : 2
    nullable: 0
    progress: 0.00%
Libplanet.Explorer.Cocona/Libplanet.Explorer.Cocona.csproj
    cs      : 3
    nullable: 0
    progress: 0.00%
Libplanet.Common/Libplanet.Common.csproj
    cs      : 15
    nullable: 3
    progress: 20.00%
Libplanet.Store/Libplanet.Store.csproj
    cs      : 43
    nullable: 6
    progress: 13.95%
Libplanet.Action/Libplanet.Action.csproj
    cs      : 62
    nullable: 3
    progress: 4.84%
Libplanet.Crypto/Libplanet.Crypto.csproj
    cs      : 12
    nullable: 0
    progress: 0.00%
Libplanet.Types/Libplanet.Types.csproj
    cs      : 68
    nullable: 0
    progress: 0.00%
# pwsh script
dotnet sln list | Where-Object {
    Test-Path -Path $_
} | ForEach-Object {
    $directory = Split-Path -Path $_ -Parent
    $isNullable = Select-Xml -Path $_ -XPath "/Project/PropertyGroup/Nullable"
    if ($isNullable) {
        $csFiles = Get-ChildItem -Path $directory -Recurse -Filter *.cs
        $nullableFiles = @($csFiles | Where-Object { Select-String -Path $_ -Pattern "#nullable disable" } | ForEach-Object { $_ })
        $progress = $nullableFiles.Length / $csFiles.Length
        Write-Host "$($_)"
        Write-Host "    cs      : $($csFiles.Length)"
        Write-Host "    nullable: $($nullableFiles.Length)"
        Write-Host "    progress: $($progress.ToString('p2'))"
    }
}

I will proceed with the task of removing the #nullable disable keyword in projects that include the <Nullable> property.

@s2quake
Copy link
Contributor

s2quake commented Jan 19, 2024

PR #3620 for stun project

@s2quake
Copy link
Contributor

s2quake commented Jan 24, 2024

#3622
Applied to Common, Action, and Explorer projects

@s2quake
Copy link
Contributor

s2quake commented Feb 5, 2024

#3644
Applied to Store project

@s2quake
Copy link
Contributor

s2quake commented Feb 7, 2024

#3651
Applied to RocksDBStore project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed suggestion Suggestion or feature request
Projects
Status: In Progress
Development

No branches or pull requests

5 participants