| Severity | Warning |
| Reported by | Metalama.Framework |
| Message | The [ImmutableType] attribute on '<Type>' has no effect, because '<Type>' derives from or implements '<ContractType>', which already requires it. Remove the attribute. |
Cause
The [ImmutableType] attribute states that a type must be written in an immutable style. The requirement is inherited: it applies to every type that derives from, or implements, a type that requires it. For instance, IAspect and Fabric require immutability, so all aspects and fabrics are checked without any attribute of their own.
Metalama reports LAMA0886 on the [ImmutableType] attribute of a type that is already bound by the contract through a base type or an interface. Checking the type gives the same result with or without the attribute, so the attribute only suggests that removing it would relax something, when nothing would change. In the message, <Type> is the type that carries the attribute, and <ContractType> is the base type or interface that already requires immutability. Metalama searches the base types first, then the interfaces. The <ContractType> isn't always marked with [ImmutableType] itself: some types, such as the validator base classes, are known to Metalama to require immutability.
In the following example, the Log aspect is marked with [ImmutableType], although it's an aspect and therefore implements IAspect.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Utilities;
3using System;
4
5namespace Doc.LAMA0886.Error;
6
7// Warning: every aspect is already bound by the immutability contract, so the attribute is redundant.
Warning LAMA0886: The [ImmutableType] attribute on 'LogAttribute' has no effect, because 'LogAttribute' derives from or implements 'IAspect', which already requires it. Remove the attribute.
8[ImmutableType]
9public class LogAttribute : OverrideMethodAspect
10{
11 public LogAttribute( string category )
12 {
13 this.Category = category;
14 }
15
16 public string Category { get; }
17
18 public override dynamic? OverrideMethod()
19 {
20 Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
21
22 return meta.Proceed();
23 }
24}
25
1namespace Doc.LAMA0886.Error;
2
3internal class OrderService
4{
5 [Log( "Orders" )]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0886.Error;
4
5internal class OrderService
6{
7 [Log("Orders")]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("[Orders] Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15
Resolution
Remove the [ImmutableType] attribute from the type named in the message. The type remains subject to the immutability rules through <ContractType>, so Metalama still reports any mutable state, as described in LAMA0880, LAMA0881, and LAMA0882.
1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0886.Fixed;
5
6// Fixed: the attribute is removed. The aspect is still checked because it implements IAspect.
7public class LogAttribute : OverrideMethodAspect
8{
9 public LogAttribute( string category )
10 {
11 this.Category = category;
12 }
13
14 public string Category { get; }
15
16 public override dynamic? OverrideMethod()
17 {
18 Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
19
20 return meta.Proceed();
21 }
22}
23
1namespace Doc.LAMA0886.Fixed;
2
3internal class OrderService
4{
5 [Log( "Orders" )]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0886.Fixed;
4
5internal class OrderService
6{
7 [Log("Orders")]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("[Orders] Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15