Skip to content

[linter] proposal: omit_obvious_native_types_in_native_fields_and_functions #59777

Open
@halildurmus

Description

@halildurmus

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

  1. Backward Compatibility: Code explicitly declaring native types will remain valid but trigger the lint warning if the type can be inferred.
  2. Exceptions: If the native type cannot be inferred from the Dart signature, explicit annotation remains necessary.
  3. Tooling: IDEs should provide quick fixes to remove unnecessary native type specifications in @Native annotations.

/cc @bwilkerson
/cc @dcharkes

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagetype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions