Description
omit_obvious_native_types_in_native_fields_and_functions
Description
Omit the native type in the @Native
annotation when it can be inferred from the annotated declaration.
Details
The @Native
annotation in dart:ffi
is used to link native fields or functions to Dart code.
With the changes introduced in https://dart-review.googlesource.com/c/sdk/+/400840, the native type for functions annotated with @Native
can now be inferred directly from their Dart signatures. (This capability already existed for fields.) As a result, explicitly specifying the native type is unnecessary in some cases, which helps make the code cleaner and more concise.
Adding a lint to promote omitting redundant native types supports Dart’s focus on clean and expressive syntax. By leveraging type inference, developers can reduce boilerplate and make FFI bindings easier to maintain and understand.
Kind
Style.
Bad Examples
import 'dart:ffi';
@Native<Pointer<Int32>>()
external Pointer<Int32> foo;
@Native<Void Function(Pointer)>()
external void bar(Pointer p);
Good Examples
import 'dart:ffi';
@Native()
external Pointer<Int32> foo;
@Native()
external void bar(Pointer p);
Considerations
- Backward Compatibility: Code explicitly declaring native types will remain valid but trigger the lint warning if the type can be inferred.
- Exceptions: If the native type cannot be inferred from the Dart signature, explicit annotation remains necessary.
- Tooling: IDEs should provide quick fixes to remove unnecessary native type specifications in
@Native
annotations.
/cc @bwilkerson
/cc @dcharkes