-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Improve nullability ergonomics for ConditionExpression cast
#5977
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
Conversation
|
Thanks for opening this pull request! |
WalkthroughAdds a conditional NotNullIfNotNull return-value attribute to the implicit string-to-ConditionExpression operator in ConditionExpression, gated by target frameworks. No runtime behavior changes; enhances nullability annotations for newer targets. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/NLog/Conditions/ConditionExpression.cs (1)
55-61: Tighten the XML docs to reflect null on null inputAdd that the operator returns null when the input is null to avoid ambiguity in generated API docs.
/// <param name="conditionExpressionText">Condition text to be converted.</param> -/// <returns>Condition expression tree.</returns> +/// <returns> +/// Condition expression tree if <paramref name="conditionExpressionText"/> is not null; +/// otherwise, null. +/// </returns>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
src/NLog/Conditions/ConditionExpression.cs(1 hunks)
🔇 Additional comments (1)
src/NLog/Conditions/ConditionExpression.cs (1)
56-58: Good use of NotNullIfNotNull and correct TFM gating
- Applying the return-attribute to the implicit operator precisely models the contract: non-null input → non-null result.
- The conditional compile guards look right for where the attribute is available. The OR_GREATER symbols behave as expected across SDKs.
References: Microsoft docs on nullable-analysis attributes and OR_GREATER preprocessor symbols, and NLog docs showing ParseExpression returns a non-null ConditionExpression for non-null input. (learn.microsoft.com, nlog-project.org)
|
Thank you for the pull-request, a really nice improvement. Going to tickle the build-servers by closing and re-opening the pull-request. |
|
|
Hooray your first pull request is merged! Thanks for the contribution! Looking for more? 👼 Please check the up-for-grabs issues - thanks! |
|
NLog v6.0.4 have now been released: Thanks again for the help with improving NLog |




This currently produces a "Possible null reference assignment. CS8601" warning:
This is because the
ConditionExpressionimplicit cast fromstring?returns aConditionExpression?(i.e. it may be null), but theConditionBasedFilter.Conditionproperty does not allow nulls.In this case though, the warning is a false positive, because the cast only actually returns null when the original string is null - and we (and the compiler) can clearly see that the string is not null. Fortunately, we can easily resolve this using the
[NotNullIfNotNull]attribute (available in .NET Core 3+ and .NET Standard 2.1).