Skip to content

Instantly share code, notes, and snippets.

@devoncarew
Last active March 3, 2023 03:40
Show Gist options
  • Select an option

  • Save devoncarew/ea346e8a6e39a900853b2bca40c829e4 to your computer and use it in GitHub Desktop.

Select an option

Save devoncarew/ea346e8a6e39a900853b2bca40c829e4 to your computer and use it in GitHub Desktop.

Rule always_declare_return_types

Group: style
State:
Since: Dart SDK: >= 2.0.0 • (Linter v0.1.4)

has-fix

Description

DO declare method return types.

When declaring a method or function always specify a return type. Declaring return types for functions helps improve your codebase by allowing the analyzer to more adequately check your code for errors that could occur during runtime.

BAD:

main() { }

_bar() => _Foo();

class _Foo {
  _foo() => 42;
}

GOOD:

void main() { }

_Foo _bar() => _Foo();

class _Foo {
  int _foo() => 42;
}

typedef predicate = bool Function(Object o);

Linter for Dart

Lint Rules

Using the Linter

Supported Lint Rules

This list is auto-generated from our sources.

Rules are organized into familiar rule groups.

  • errors - Possible coding errors.

  • style - Matters of style, largely derived from the official Dart Style Guide.

  • pub - Pub-related rules.

In addition, rules can be further distinguished by maturity. Unqualified rules are considered stable, while others may be marked experimental to indicate that they are under review. Lints that are marked as deprecated should not be used and are subject to removal in future Linter releases.

Rules can be selectively enabled in the analyzer using analysis options or through an analysis options file.

  • An auto-generated list enabling all options is provided here.

As some lints may contradict each other, only a subset of these will be enabled in practice, but this list should provide a convenient jumping-off point.

Many lints are included in various predefined rulesets:

  • core for official "core" Dart team lint rules.
  • recommended for additional lint rules "recommended" by the Dart team.
  • flutter for rules recommended for Flutter projects (flutter create enables these by default).

Rules included in these rulesets are badged in the documentation below.

These rules are under active development. Feedback is welcome!

Error Rules

Rule Description
always_use_package_imports Avoid relative imports for files in lib/. has-fix
avoid_dynamic_calls Avoid method calls or property accesses on a "dynamic" target.
avoid_empty_else Avoid empty else statements. core recommended flutter has-fix
avoid_print Avoid print calls in production code. flutter has-fix
avoid_relative_lib_imports Avoid relative imports for files in lib/. core recommended flutter has-fix
avoid_returning_null_for_future Avoid returning null for Future. has-fix
avoid_slow_async_io Avoid slow async dart:io methods.
avoid_type_to_string Avoid .toString() in production code since results may be minified.
avoid_types_as_parameter_names Avoid types as parameter names. core recommended flutter has-fix
avoid_web_libraries_in_flutter Avoid using web-only libraries outside Flutter web plugin packages. flutter
cancel_subscriptions Cancel instances of dart.async.StreamSubscription.
close_sinks Close instances of dart.core.Sink.
collection_methods_unrelated_type Invocation of various collection methods with arguments of unrelated types.
comment_references Only reference in scope identifiers in doc comments.
control_flow_in_finally Avoid control flow in finally blocks. recommended flutter
diagnostic_describe_all_properties DO reference all public properties in debug methods. has-fix
discarded_futures Don't invoke asynchronous functions in non-async blocks. has-fix
empty_statements Avoid empty statements. recommended flutter has-fix
hash_and_equals Always override hashCode if overriding ==. core recommended flutter has-fix
invalid_case_patterns Use case expressions that are valid in Dart 3.0. has-fix
invariant_booleans Conditions should not unconditionally evaluate to true or to false.
iterable_contains_unrelated_type Invocation of Iterable.contains with references of unrelated types. core recommended flutter
list_remove_unrelated_type Invocation of remove with references of unrelated types. core recommended flutter
literal_only_boolean_expressions Boolean expression composed only with literals.
no_adjacent_strings_in_list Don't use adjacent strings in list.
no_duplicate_case_values Don't use more than one case with same value. core recommended flutter has-fix
no_logic_in_create_state Don't put any logic in createState. flutter
prefer_relative_imports Prefer relative imports for files in lib/. has-fix
prefer_void_to_null Don't use the Null type, unless you are positive that you don't want void. recommended flutter has-fix
test_types_in_equals Test type arguments in operator ==(Object other).
throw_in_finally Avoid throw in finally block.
unnecessary_statements Avoid using unnecessary statements.
unrelated_type_equality_checks Equality operator == invocation with references of unrelated types. core recommended flutter
unsafe_html Avoid unsafe HTML APIs.
use_build_context_synchronously Do not use BuildContexts across async gaps. flutter
use_key_in_widget_constructors Use key in widget constructors. flutter has-fix
valid_regexps Use valid regular expression syntax. core recommended flutter

Style Rules

Rule Description
always_declare_return_types Declare method return types. has-fix
always_put_control_body_on_new_line Separate the control structure expression from its statement. has-fix
always_put_required_named_parameters_first Put required named parameters first. has-fix
always_require_non_null_named_parameters Specify @required on named parameters without defaults. recommended flutter has-fix
always_specify_types Specify type annotations. has-fix
annotate_overrides Annotate overridden members. recommended flutter has-fix
avoid_annotating_with_dynamic Avoid annotating with dynamic when not required. has-fix
avoid_as Avoid using as.
avoid_bool_literals_in_conditional_expressions Avoid bool literals in conditional expressions.
avoid_catches_without_on_clauses Avoid catches without on clauses.
avoid_catching_errors Don't explicitly catch Error or types that implement it.
avoid_classes_with_only_static_members Avoid defining a class that contains only static members.
avoid_double_and_int_checks Avoid double and int checks.
avoid_equals_and_hash_code_on_mutable_classes Avoid overloading operator == and hashCode on classes not marked @immutable.
avoid_escaping_inner_quotes Avoid escaping inner quotes by converting surrounding quotes. has-fix
avoid_field_initializers_in_const_classes Avoid field initializers in const classes.
avoid_final_parameters Avoid final for parameter declarations.
avoid_function_literals_in_foreach_calls Avoid using forEach with a function literal. recommended flutter has-fix
avoid_implementing_value_types Don't implement classes that override ==.
avoid_init_to_null Don't explicitly initialize variables to null. recommended flutter has-fix
avoid_js_rounded_ints Avoid JavaScript rounded ints.
avoid_multiple_declarations_per_line Don't declare multiple variables on a single line.
avoid_null_checks_in_equality_operators Don't check for null in custom == operators. recommended flutter has-fix
avoid_positional_boolean_parameters Avoid positional boolean parameters.
avoid_private_typedef_functions Avoid private typedef functions. has-fix
avoid_redundant_argument_values Avoid redundant argument values. has-fix
avoid_renaming_method_parameters Don't rename parameters of overridden methods. recommended flutter has-fix
avoid_return_types_on_setters Avoid return types on setters. recommended flutter has-fix
avoid_returning_null Avoid returning null from members whose return type is bool, double, int, or num.
avoid_returning_null_for_void Avoid returning null for void. recommended flutter has-fix
avoid_returning_this Avoid returning this from methods just to enable a fluent interface.
avoid_setters_without_getters Avoid setters without getters.
avoid_shadowing_type_parameters Avoid shadowing type parameters. core recommended flutter
avoid_single_cascade_in_expression_statements Avoid single cascade in expression statements. recommended flutter has-fix
avoid_types_on_closure_parameters Avoid annotating types for function expression parameters. has-fix
avoid_unnecessary_containers Avoid unnecessary containers. flutter has-fix
avoid_unused_constructor_parameters Avoid defining unused parameters in constructors. has-fix
avoid_void_async Avoid async functions that return void. has-fix
await_only_futures Await only futures. core recommended flutter has-fix
camel_case_extensions Name extensions using UpperCamelCase. core recommended flutter
camel_case_types Name types using UpperCamelCase. core recommended flutter
cascade_invocations Cascade consecutive method invocations on the same reference. has-fix
cast_nullable_to_non_nullable Don't cast a nullable value to a non nullable type.
combinators_ordering Sort combinator names alphabetically. has-fix
conditional_uri_does_not_exist Missing conditional import.
constant_identifier_names Prefer using lowerCamelCase for constant names. recommended flutter
curly_braces_in_flow_control_structures DO use curly braces for all flow control structures. core recommended flutter has-fix
dangling_library_doc_comments Attach library doc comments to library directives. has-fix
deprecated_consistency Missing deprecated annotation.
directives_ordering Adhere to Effective Dart Guide directives sorting conventions. has-fix
do_not_use_environment Do not use environment declared variables.
empty_catches Avoid empty catch blocks. core recommended flutter has-fix
empty_constructor_bodies Use ; instead of {} for empty constructor bodies. recommended flutter has-fix
enable_null_safety Do use sound null safety.
eol_at_end_of_file Put a single newline at end of file. has-fix
exhaustive_cases Define case clauses for all constants in enum-like classes. recommended flutter has-fix
file_names Name source files using lowercase_with_underscores. core recommended flutter
flutter_style_todos Use Flutter TODO format: // TODO(username): message, https://URL-to-issue.
implementation_imports Don't import implementation files from another package. recommended flutter
implicit_call_tearoffs Explicitly tear-off call methods when using an object as a Function. has-fix
join_return_with_assignment Join return statement with assignment when possible.
leading_newlines_in_multiline_strings Start multiline strings with a newline. has-fix
library_annotations Attach library annotations to library directives. has-fix
library_names Name libraries using lowercase_with_underscores. recommended flutter
library_prefixes Use lowercase_with_underscores when specifying a library prefix. recommended flutter
library_private_types_in_public_api Avoid using private types in public APIs. recommended flutter
lines_longer_than_80_chars Avoid lines longer than 80 characters.
missing_whitespace_between_adjacent_strings Missing whitespace between adjacent strings.
no_default_cases No default cases.
no_leading_underscores_for_library_prefixes Avoid leading underscores for library prefixes. recommended flutter has-fix
no_leading_underscores_for_local_identifiers Avoid leading underscores for local identifiers. recommended flutter has-fix
no_runtimeType_toString Avoid calling toString() on runtimeType.
non_constant_identifier_names Name non-constant identifiers using lowerCamelCase. core recommended flutter has-fix
noop_primitive_operations Noop primitive operations. has-fix
null_check_on_nullable_type_parameter Don't use null check on a potentially nullable type parameter. core recommended flutter has-fix
null_closures Do not pass null as an argument where a closure is expected. recommended flutter has-fix
omit_local_variable_types Omit type annotations for local variables. has-fix
one_member_abstracts Avoid defining a one-member abstract class when a simple function will do.
only_throw_errors Only throw instances of classes extending either Exception or Error.
overridden_fields Don't override fields. recommended flutter
package_api_docs Provide doc comments for all public APIs.
package_prefixed_library_names Prefix library names with the package name and a dot-separated path. core recommended flutter
parameter_assignments Don't reassign references to parameters of functions or methods.
prefer_adjacent_string_concatenation Use adjacent strings to concatenate string literals. recommended flutter has-fix
prefer_asserts_in_initializer_lists Prefer putting asserts in initializer lists.
prefer_asserts_with_message Prefer asserts with message.
prefer_bool_in_asserts Prefer using a boolean as the assert condition.
prefer_collection_literals Use collection literals when possible. recommended flutter has-fix
prefer_conditional_assignment Prefer using ??= over testing for null. recommended flutter has-fix
prefer_const_constructors Prefer const with constant constructors. flutter has-fix
prefer_const_constructors_in_immutables Prefer declaring const constructors on @immutable classes. flutter has-fix
prefer_const_declarations Prefer const over final for declarations. flutter has-fix
prefer_const_literals_to_create_immutables Prefer const literals as parameters of constructors on @immutable classes. flutter has-fix
prefer_constructors_over_static_methods Prefer defining constructors instead of static methods to create instances.
prefer_contains Use contains for List and String instances. recommended flutter has-fix
prefer_double_quotes Prefer double quotes where they won't require escape sequences. has-fix
prefer_equal_for_default_values Use = to separate a named parameter from its default value. recommended flutter
prefer_expression_function_bodies Use => for short members whose body is a single return statement. has-fix
prefer_final_fields Private field could be final. recommended flutter has-fix
prefer_final_in_for_each Prefer final in for-each loop variable if reference is not reassigned. has-fix
prefer_final_locals Prefer final for variable declarations if they are not reassigned. has-fix
prefer_final_parameters Prefer final for parameter declarations if they are not reassigned. has-fix
prefer_for_elements_to_map_fromIterable Prefer 'for' elements when building maps from iterables. recommended flutter has-fix
prefer_foreach Use forEach to only apply a function to all the elements.
prefer_function_declarations_over_variables Use a function declaration to bind a function to a name. recommended flutter has-fix
prefer_generic_function_type_aliases Prefer generic function type aliases. core recommended flutter has-fix
prefer_if_elements_to_conditional_expressions Prefer if elements to conditional expressions where possible. has-fix
prefer_if_null_operators Prefer using if null operators. recommended flutter has-fix
prefer_initializing_formals Use initializing formals when possible. recommended flutter has-fix
prefer_inlined_adds Inline list item declarations where possible. recommended flutter has-fix
prefer_int_literals Prefer int literals over double literals. has-fix
prefer_interpolation_to_compose_strings Use interpolation to compose strings and values. recommended flutter has-fix
prefer_is_empty Use isEmpty for Iterables and Maps. core recommended flutter has-fix
prefer_is_not_empty Use isNotEmpty for Iterables and Maps. core recommended flutter has-fix
prefer_is_not_operator Prefer is! operator. recommended flutter has-fix
prefer_iterable_whereType Prefer to use whereType on iterable. core recommended flutter has-fix
prefer_mixin Prefer using mixins.
prefer_null_aware_method_calls Prefer null aware method calls.
prefer_null_aware_operators Prefer using null aware operators. recommended flutter has-fix
prefer_single_quotes Only use double quotes for strings containing single quotes. has-fix
prefer_spread_collections Use spread collections when possible. recommended flutter has-fix
prefer_typing_uninitialized_variables Prefer typing uninitialized variables and fields. core recommended flutter has-fix
provide_deprecation_message Provide a deprecation message, via @Deprecated("message"). core recommended flutter
public_member_api_docs Document all public members.
recursive_getters Property getter recursively returns itself. recommended flutter
require_trailing_commas Use trailing commas for all function calls and declarations. has-fix
sized_box_for_whitespace SizedBox for whitespace. flutter has-fix
sized_box_shrink_expand Use SizedBox shrink and expand named constructors.
slash_for_doc_comments Prefer using /// for doc comments. recommended flutter has-fix
sort_child_properties_last Sort child properties last in widget instance creations. flutter has-fix
sort_constructors_first Sort constructor declarations before other members. has-fix
sort_unnamed_constructors_first Sort unnamed constructor declarations first. has-fix
super_goes_last Place the super call last in a constructor initialization list.
tighten_type_of_initializing_formals Tighten type of initializing formal.
type_annotate_public_apis Type annotate public APIs. has-fix
type_init_formals Don't type annotate initializing formals. recommended flutter has-fix
unawaited_futures Future results in async function bodies must be awaited or marked unawaited using dart:async. has-fix
unnecessary_await_in_return Unnecessary await keyword in return.
unnecessary_brace_in_string_interps Avoid using braces in interpolation when not needed. recommended flutter has-fix
unnecessary_breaks Don't use explicit breaks when a break is implied. has-fix
unnecessary_const Avoid const keyword. recommended flutter has-fix
unnecessary_constructor_name Unnecessary .new constructor name. recommended flutter has-fix
unnecessary_final Don't use final for local variables. has-fix
unnecessary_getters_setters Avoid wrapping fields in getters and setters just to be "safe". recommended flutter has-fix
unnecessary_lambdas Don't create a lambda when a tear-off will do. has-fix
unnecessary_late Don't specify the late modifier when it is not needed. recommended flutter has-fix
unnecessary_library_directive Avoid library directives unless they have documentation comments or annotations. has-fix
unnecessary_new Unnecessary new keyword. recommended flutter has-fix
unnecessary_null_aware_assignments Avoid null in null-aware assignment. recommended flutter has-fix
unnecessary_null_aware_operator_on_extension_on_nullable Unnecessary null aware operator on extension on a nullable type.
unnecessary_null_checks Unnecessary null checks.
unnecessary_null_in_if_null_operators Avoid using null in if null operators. recommended flutter has-fix
unnecessary_nullable_for_final_variable_declarations Use a non-nullable type for a final variable initialized with a non-nullable value. recommended flutter has-fix
unnecessary_overrides Don't override a method to do a super method invocation with the same parameters. core recommended flutter has-fix
unnecessary_parenthesis Unnecessary parentheses can be removed. has-fix
unnecessary_raw_strings Unnecessary raw string. has-fix
unnecessary_string_escapes Remove unnecessary backslashes in strings. recommended flutter has-fix
unnecessary_string_interpolations Unnecessary string interpolation. recommended flutter has-fix
unnecessary_this Don't access members with this unless avoiding shadowing. recommended flutter has-fix
unnecessary_to_list_in_spreads Unnecessary toList() in spreads.
unreachable_from_main Unreachable top-level members in executable libraries.
use_colored_box Use ColoredBox.
use_decorated_box Use DecoratedBox. has-fix
use_enums Use enums rather than classes that behave like enums. has-fix
use_full_hex_values_for_flutter_colors Prefer an 8-digit hexadecimal integer(0xFFFFFFFF) to instantiate Color. flutter has-fix
use_function_type_syntax_for_parameters Use generic function type syntax for parameters. recommended flutter has-fix
use_if_null_to_convert_nulls_to_bools Use if-null operators to convert nulls to bools.
use_is_even_rather_than_modulo Prefer intValue.isOdd/isEven instead of checking the result of % 2.
use_late_for_private_fields_and_variables Use late for private members with a non-nullable type.
use_named_constants Use predefined named constants.
use_raw_strings Use raw string to avoid escapes. has-fix
use_rethrow_when_possible Use rethrow to rethrow a caught exception. recommended flutter has-fix
use_setters_to_change_properties Use a setter for operations that conceptually change a property.
use_string_buffers Use string buffers to compose strings.
use_string_in_part_of_directives Use string in part of directives.
use_super_parameters Use super-initializer parameters where possible. has-fix
use_test_throws_matchers Use throwsA matcher instead of fail().
use_to_and_as_if_applicable Start the name of the method with to/_to or as/_as if applicable.
void_checks Don't assign to void. core recommended flutter

Pub Rules

Rule Description
depend_on_referenced_packages Depend on referenced packages. core recommended flutter
package_names Use lowercase_with_underscores for package names. recommended flutter
secure_pubspec_urls Use secure urls in pubspec.yaml.
sort_pub_dependencies Sort pub dependencies alphabetically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment