| Severity | Warning |
| Reported by | Metalama.Framework |
| Message | '<Type>' must be written in an immutable style because it is an aspect, a fabric, a validator, or another type bound by the [ImmutableType] contract, but '<MemberType>' is not marked [ImmutableType]. Marking it requires every implementation to be immutable, which Metalama verifies. Path: <Path>. |
Cause
Aspects, fabrics, validators, and the types that you mark with the [ImmutableType] attribute must be written in an immutable style, because an aspect instance is reused for all its targets. See LAMA0882 for the rationale and for the rules that decide whether a type is immutable.
LAMA0884 is the variant of LAMA0882 for a field or automatic property whose type is an interface or an abstract class that isn't marked with [ImmutableType] and that Metalama doesn't know. A typical example is a field of type IEnumerable<string> or IReadOnlyList<string>: the interface doesn't prevent the underlying object from being a List<string> or an array that someone modifies later.
Metalama reports a separate diagnostic because the remedy is different. An interface has no members to verify, so marking it with [ImmutableType] doesn't check anything on the interface itself. It instead requires every type that implements the interface to be written in an immutable style, and Metalama verifies each implementation in turn. This guarantee reaches only the implementations that are compiled with Metalama, so you may decide to weigh this rule differently from the others, for instance by changing its severity in an .editorconfig file.
In the message, <Type> is the type that must be immutable and <MemberType> is the type of the member. When the unmarked interface is a type argument, for example in ImmutableArray<IFormatter>, <MemberType> is the whole member type, and <Path> shows which type isn't marked. <Path> is the chain from <Type> to that type, for example LogAttribute -> _categories -> IReadOnlyList<string> (an interface or abstract type that is not marked [ImmutableType]).
In the following example, the Log aspect stores its categories in an IReadOnlyList<string> field, which holds the array passed to the constructor.
1using Metalama.Framework.Aspects;
2using System;
3using System.Collections.Generic;
4
5namespace Doc.LAMA0884.Error;
6
7public class LogAttribute : OverrideMethodAspect
8{
9 // Warning: the object behind IReadOnlyList<string> may still be modified.
Warning LAMA0884: 'LogAttribute' must be written in an immutable style because it is an aspect, a fabric, a validator, or another type bound by the [ImmutableType] contract, but 'IReadOnlyList<string>' is not marked [ImmutableType]. Marking it requires every implementation to be immutable, which Metalama verifies. Path: LogAttribute -> _categories -> IReadOnlyList<string> (an interface or abstract type that is not marked [ImmutableType]).
10 private readonly IReadOnlyList<string> _categories;
11
12 public LogAttribute( params string[] categories )
13 {
14 this._categories = categories;
15 }
16
17 public override dynamic? OverrideMethod()
18 {
19 var categories = string.Join( ", ", this._categories );
20 Console.WriteLine( $"[{categories}] Entering {meta.Target.Method}." );
21
22 return meta.Proceed();
23 }
24}
25
1namespace Doc.LAMA0884.Error;
2
3internal class OrderService
4{
5 [Log( "Orders", "Billing" )]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0884.Error;
4
5internal class OrderService
6{
7 [Log("Orders", "Billing")]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("[Orders, Billing] Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15
Resolution
- Replace the interface with a concrete immutable type, such as
ImmutableArray<string>instead ofIEnumerable<string>orIReadOnlyList<string>. - If the interface is yours, mark it with
[ImmutableType]. Every implementation must then be immutable, and Metalama reports the implementations that aren't, with LAMA0880, LAMA0881 or LAMA0882. - If the interface isn't yours and you know that every value you store in it is immutable, declare it with a
MetalamaImmutableTypeitem in the project file (see LAMA0885). - If the warning is unwanted, lower its severity in an
.editorconfigfile, or suppress it with#pragma warning disable LAMA0884.
In the following example, the field is an ImmutableArray<string>, a copy of the constructor argument that can't be modified.
1using Metalama.Framework.Aspects;
2using System;
3using System.Collections.Immutable;
4
5namespace Doc.LAMA0884.Fixed;
6
7public class LogAttribute : OverrideMethodAspect
8{
9 // Fixed: ImmutableArray<string> is a concrete immutable type.
10 private readonly ImmutableArray<string> _categories;
11
12 public LogAttribute( params string[] categories )
13 {
14 this._categories = ImmutableArray.Create( categories );
15 }
16
17 public override dynamic? OverrideMethod()
18 {
19 var categories = string.Join( ", ", this._categories );
20 Console.WriteLine( $"[{categories}] Entering {meta.Target.Method}." );
21
22 return meta.Proceed();
23 }
24}
25
1namespace Doc.LAMA0884.Fixed;
2
3internal class OrderService
4{
5 [Log( "Orders", "Billing" )]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0884.Fixed;
4
5internal class OrderService
6{
7 [Log("Orders", "Billing")]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("[Orders, Billing] Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15