Skip to main content

negative_variable_dimension

The variable dimension of a variable-length array must be non-negative.

Description

#

The analyzer produces this diagnostic in two cases.

The first is when the variable dimension given in an Array.variableWithVariableDimension annotation is negative. The variable dimension is the first argument in the annotation.

The second is when the variable dimension given in an Array.variableMulti annotation is negative. The variable dimension is specified in the variableDimension argument of the annotation.

For more information about FFI, see C interop using dart:ffi.

Examples

#

The following code produces this diagnostic because a variable dimension of -1 was provided in the Array.variableWithVariableDimension annotation:

dart
import 'dart:ffi';

final class MyStruct extends Struct {
  @Array.variableWithVariableDimension(-1)
  external Array<Uint8> a0;
}

The following code produces this diagnostic because a variable dimension of -1 was provided in the Array.variableMulti annotation:

dart
import 'dart:ffi';

final class MyStruct2 extends Struct {
  @Array.variableMulti(variableDimension: -1, [1, 2])
  external Array<Array<Array<Uint8>>> a0;
}

Common fixes

#

Change the variable dimension with zero (0) or a positive number:

dart
import 'dart:ffi';

final class MyStruct extends Struct {
  @Array.variableWithVariableDimension(1)
  external Array<Uint8> a0;
}

Change the variable dimension with zero (0) or a positive number:

dart
import 'dart:ffi';

final class MyStruct2 extends Struct {
  @Array.variableMulti(variableDimension: 1, [1, 2])
  external Array<Array<Array<Uint8>>> a0;
}