MustUse.Analyzer
1.0.0
dotnet add package MustUse.Analyzer --version 1.0.0
NuGet\Install-Package MustUse.Analyzer -Version 1.0.0
<PackageReference Include="MustUse.Analyzer" Version="1.0.0" />
paket add MustUse.Analyzer --version 1.0.0
#r "nuget: MustUse.Analyzer, 1.0.0"
// Install MustUse.Analyzer as a Cake Addin #addin nuget:?package=MustUse.Analyzer&version=1.0.0 // Install MustUse.Analyzer as a Cake Tool #tool nuget:?package=MustUse.Analyzer&version=1.0.0
MustUse
Attribute
Introduction
This project introduces the MustUse
attribute to C#. MustUse
can be applied to types and methods to encourage the
usage of their return values. It helps prevent needless computation and ensures that the caller handles the returned
value appropriately, especially in cases where the return type represents a result or an error that should be handled.
Inspiration
This attribute is inspired by the #[must_use]
attribute in Rust, which serves a similar purpose of ensuring that the
result of a function or method is used and not ignored.
Usage
To use the MustUse
attribute, simply apply it to a type or method declaration, providing an optional message as a
parameter. The message can be used to provide additional context or guidance to the caller.
Applying to Types:
[MustUse("The result of this operation should be handled.")]
public class Result
{
// Class members...
}
When the MustUse
attribute is applied to a type, it indicates that the instances of that type should be used and not
ignored. If the return value of a method or function returning this type is not used, a warning will be displayed.
Applying to Methods:
[MustUse("`AddFive` is pure, so ignoring the result indicates your usage is a waste of computation")]
public int AddFive(int x)
{
return x + 5;
}
When the MustUse
attribute is applied to a method, it indicates that the return value of that method should be used
and not ignored. If the caller does not use the returned value, a warning will be displayed.
Benefits
Using the MustUse
attribute provides several benefits:
- It helps prevent needless computation by ensuring that the caller uses the returned value of a method or function.
- It encourages proper handling of return types, especially when dealing with result types or error conditions.
- It improves code quality and readability by making the intent explicit and providing guidance to the caller.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MustUse.Analyzer:
Package | Downloads |
---|---|
IronVelo
The IronVelo C# SDK provides a robust and user-friendly interface for integrating with IronVelo's Identity Provider (IdP), as well as utilities for secure programming. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 186 | 6/21/2024 |