Skip to content

Enable nullable references in Moq.csproj #1535

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

Open
wants to merge 92 commits into
base: main
Choose a base branch
from

Conversation

andrewimcclement
Copy link

@andrewimcclement andrewimcclement commented Feb 26, 2025

Aims to resolve #1418 , #1416 .

Does not extend to Moq.Tests.csproj, etc.

Nullable reference issues are enabled as warnings in Moq.csproj. Several issues have been left unresolved, as I lack the knowledge to resolve them correctly at this time (perhaps maintainers can help here, or else it may be acceptable to put this PR down as is and slowly pick off the remaining nullable reference warnings one at a time).

In general I have tried hard to avoid actually changing the code functionality, except where it seemed clear. So several simplifications could be made in follow up PRs.

See the docs for details of the nullability attributes I have used.

@CLAassistant
Copy link

CLAassistant commented Feb 26, 2025

CLA assistant check
All committers have signed the CLA.

@kzu kzu force-pushed the aim/nullable-enable branch from edc86ce to 0b65989 Compare May 26, 2025 11:51
@kzu
Copy link
Member

kzu commented May 26, 2025

To fix the dotnet-format build error, plz run:

dotnet format whitespace -v:diag --exclude ~/.nuget; dotnet format style -v:diag --exclude ~/.nuget

@kzu kzu force-pushed the aim/nullable-enable branch from 0b65989 to fc1261d Compare May 26, 2025 19:00
@kzu kzu added the tech debt label May 26, 2025
@kzu kzu force-pushed the aim/nullable-enable branch from fc1261d to c1d2a24 Compare May 26, 2025 19:16
@andrewimcclement
Copy link
Author

Run the dotnet format command.

…hing ever calls `new Condition(null)` so we can simply rely on nullable annotations, so long as we guard the public interface.
@kzu kzu force-pushed the aim/nullable-enable branch from c75cc42 to b14b04a Compare July 7, 2025 18:18
@kzu kzu requested a review from Copilot July 7, 2025 18:26
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Enables nullable reference types in Moq.csproj and applies nullable annotations across the codebase

  • Turn on <Nullable>enable</Nullable> and define NULLABLE_REFERENCE_TYPES in the project file
  • Annotate thousands of reference-type members with ?, add null-forgiving operators (!), and nullability attributes
  • Suppress nullable warnings (default!) and leave TODOs where nullable behavior needs further attention

Reviewed Changes

Copilot reviewed 78 out of 78 changed files in this pull request and generated 2 comments.

File Description
src/Moq/Mock`1.cs Contains a leftover TODO in the parameterless constructor
src/Moq/StringBuilderExtensions.cs Manually disposes enumerator instead of using a using or foreach

@@ -110,6 +111,7 @@ internal Mock(bool skipInitialize)
// The skipInitialize parameter is not used at all, and it's
// just to differentiate this ctor that should do nothing
// from the regular ones which initializes the proxy, etc.
// TODO: How should nullable references be handled here?
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

Resolve or remove this TODO. Clarify how nullable references should be handled in this constructor (e.g., initialize instance or document intent) to avoid leaving incomplete code.

Copilot uses AI. Check for mistakes.

@@ -148,6 +148,8 @@ public static StringBuilder AppendValueOf(this StringBuilder stringBuilder, obje

stringBuilder.AppendValueOf(enumerator.Current);
}

(enumerator as IDisposable)?.Dispose();
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using a foreach loop or wrapping the enumerator in a using statement to ensure deterministic disposal even if an exception occurs during iteration.

Copilot uses AI. Check for mistakes.

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

Successfully merging this pull request may close these issues.

The public API is still missing nullable reference type annotations
3 participants