Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable unawaited_futures, ship fireAndForget #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<img src="https://user-images.githubusercontent.com/1096485/66209493-bc0ec900-e6b7-11e9-80c6-222e778f0c8d.png">
</p>

`lint` is a hand-picked, open-source, community-driven collection of lint rules for Dart and Flutter projects.
`lint` is an opinionated, hand-picked, open-source, community-driven **collection** of lint rules for Dart and Flutter projects.
The set of rules follows the [Effective Dart: Style Guide](https://dart.dev/guides/language/effective-dart/style).

This package can be used as a replacement for [`package:pedantic`](https://github.com/dart-lang/pedantic) for those who prefer stricter rules.
Expand Down
23 changes: 20 additions & 3 deletions lib/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,29 @@ linter:
# https://dart-lang.github.io/linter/lints/type_init_formals.html
- type_init_formals

# Too many false positives.
# Using the pedantic package for the unawaited function doesn't make code better readable
# Catches common mistakes where `await` was accidentally forgotten.
# The lint can be ignored by wrapping the Future in any function or calling a function on it. Lint provides the
# `fireAndForget` function and extension which can be used.
#
# ```
# Future<void> logAsync(String msg) async { /*...*/ }
#
# Future<void> someOperation() async {
# await doSomething();
#
# // This alone triggers the lint because await is missing
# logAsync('success');
#
# // The fireAndForget() extensions signals that await is intentionally missing
# logAsync('success').fireAndForget();
#
# // The fireAndForget() function signals that await is intentionally missing
# fireAndForget(logAsync('success'));
# }
# ```
# pedantic: enabled
# https://dart-lang.github.io/linter/lints/unawaited_futures.html
# - unawaited_futures
- unawaited_futures

# Remove async/await clutter when not required
# https://dart-lang.github.io/linter/lints/unnecessary_await_in_return.html
Expand Down
66 changes: 66 additions & 0 deletions lib/fire_and_forget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/// Indicates to tools that [future] is intentionally not `await`-ed.
///
/// In an `async` context, it is normally expected that all [Future]s are
/// awaited, and that is the basis of the lint `unawaited_futures`. However,
/// there are times where one or more futures are intentionally not awaited.
/// This function may be used to ignore a particular future. It silences the
/// `unawaited_futures` lint.
///
/// ## Usage
///
/// ```dart
/// Future<void> logAsync(String msg) async { /*...*/ }
///
/// Future<void> someOperation() async {
/// await doSomething();
///
/// // This alone triggers the lint because await is missing
/// logAsync('success');
///
/// // The fireAndForget() function signals that await is intentionally missing
/// fireAndForget(logAsync('success'));
/// }
/// ```
///
/// ## Extension vs. wrapper function
///
/// `fireAndForget` can also be used as extension, see [FireAndForget].
///
/// The extensions should be preferred as it is
/// - easier to read and
/// - doesn't break formatting of method chains.
/// src: https://github.com/dart-lang/sdk/issues/32280#issuecomment-372060804
///
/// The top-level function is useful because it gets autocompleted in IDEs without writing the import first.
/// This is still not possible for extension functions https://github.com/dart-lang/sdk/issues/38894.
void fireAndForget<T>(Future<void>? future) {}

/// Hosts the `fireAndForget` extension to silence the `unawaited_futures` warning.
extension FireAndForget on Future<void>? {
/// Indicates to tools that [Future] is intentionally not `await`-ed.
///
/// In an `async` context, it is normally expected that all [Future]s are
/// awaited, and that is the basis of the lint `unawaited_futures`. However,
/// there are times where one or more futures are intentionally not awaited.
/// This function may be used to ignore a particular future. It silences the
/// `unawaited_futures` lint.
///
/// ## Usage
///
/// ```dart
/// Future<void> logAsync(String msg) async { /*...*/ }
///
/// Future<void> someOperation() async {
/// await doSomething();
///
/// // This alone triggers the lint because await is missing
/// logAsync('success');
///
/// // The fireAndForget() extensions signals that await is intentionally missing
/// logAsync('success').fireAndForget();
/// }
/// ```
///
/// `fireAndForget` is also available as top-level function `fireAndForget()` which wraps a Future
void fireAndForget() {}
}
4 changes: 3 additions & 1 deletion lib/lint.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// The lint package doesn't ship any dart source code.
/// The lint package ships an opinionated, community-driven set of lint rules for Dart and Flutter projects. Like pedantic but stricter
///
/// To enable `lint`,
/// 1. Add it to your dev_dependencies
Expand All @@ -12,3 +12,5 @@
/// include: package:lint/analysis_options.yaml
/// ```
library lint;

export 'package:lint/fire_and_forget.dart';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: lint
version: 1.5.3
description: An opiniated, community-driven set of lint rules for Dart and Flutter projects. Like pedantic but stricter
description: An opinionated, community-driven set of lint rules for Dart and Flutter projects. Like pedantic but stricter
homepage: https://github.com/passsy/dart-lint

environment:
Expand Down