avoid-incorrect-uri

Path
lint/suspicious/avoid-incorrect-uri
Default
Off, opt in
Origin
Falcon
Rule
74 of 512

Reject runtime-invalid Uri constructor arguments: conflicting path or query inputs and query parameter values that are neither null, strings nor iterables of strings. Only resolved dart:core Uri constructors are checked; unknown values are skipped.

Invalid

example.dartdart
import 'dart:core';
import 'dart:core' as core;

void f(int count) {
  Uri(queryParameters: {'count': 1});
  Uri.http('example.com', '/', {'flag': false});
  Uri.https('example.com', '/', {'count': 1.5});
  Uri(path: '', pathSegments: ['']);
  new Uri(queryParameters: {'count': count});
  core.Uri(queryParameters: {'count': 3});
  core.Uri.https('example.com', '/', {'values': ['valid', 2]});
  Uri(queryParameters: {'values': [null]});
  Uri(query: 'key=value', queryParameters: {'key': 'value'});
}

void collections(bool includeCount, List<int> counts) {
  Uri(queryParameters: {if (includeCount) 'count': 1});
  Uri(queryParameters: {
    if (includeCount) 'count': 1
    else 'other': false,
    for (final count in counts) 'count$count': 2,
    for (final count in counts)
      if (includeCount) 'nested$count': 1,
  });
}
`Uri` query parameter values must be `null`, strings or iterables of strings.
4void f(int count) {
5 Uri(queryParameters: {'count': 1});
`Uri` query parameter values must be `null`, strings or iterables of strings.
5 Uri(queryParameters: {'count': 1});
6 Uri.http('example.com', '/', {'flag': false});
`Uri` query parameter values must be `null`, strings or iterables of strings.
6 Uri.http('example.com', '/', {'flag': false});
7 Uri.https('example.com', '/', {'count': 1.5});
Do not provide both `path` and `pathSegments` to `Uri`.
7 Uri.https('example.com', '/', {'count': 1.5});
8 Uri(path: '', pathSegments: ['']);
`Uri` query parameter values must be `null`, strings or iterables of strings.
8 Uri(path: '', pathSegments: ['']);
9 new Uri(queryParameters: {'count': count});
`Uri` query parameter values must be `null`, strings or iterables of strings.
9 new Uri(queryParameters: {'count': count});
10 core.Uri(queryParameters: {'count': 3});
`Uri` query parameter values must be `null`, strings or iterables of strings.
10 core.Uri(queryParameters: {'count': 3});
11 core.Uri.https('example.com', '/', {'values': ['valid', 2]});
`Uri` query parameter values must be `null`, strings or iterables of strings.
11 core.Uri.https('example.com', '/', {'values': ['valid', 2]});
12 Uri(queryParameters: {'values': [null]});
Do not provide both `query` and `queryParameters` to `Uri`.
12 Uri(queryParameters: {'values': [null]});
13 Uri(query: 'key=value', queryParameters: {'key': 'value'});
`Uri` query parameter values must be `null`, strings or iterables of strings.
16void collections(bool includeCount, List<int> counts) {
17 Uri(queryParameters: {if (includeCount) 'count': 1});
`Uri` query parameter values must be `null`, strings or iterables of strings.
18 Uri(queryParameters: {
19 if (includeCount) 'count': 1
`Uri` query parameter values must be `null`, strings or iterables of strings.
19 if (includeCount) 'count': 1
20 else 'other': false,
`Uri` query parameter values must be `null`, strings or iterables of strings.
20 else 'other': false,
21 for (final count in counts) 'count$count': 2,
`Uri` query parameter values must be `null`, strings or iterables of strings.
22 for (final count in counts)
23 if (includeCount) 'nested$count': 1,

Valid

example.dartdart
import 'dart:core';
import 'dart:core' as core;

void f(dynamic unknown, Iterable<String> values, String? path) {
  Uri(fragment: '', queryParameters: {'hello': 'world'});
  Uri.https('example.com', '/', {'hello': 'world'});
  Uri.http('example.com', '/', {'hello': ['world', 'again']});
  Uri(path: '');
  Uri(pathSegments: ['']);
  Uri(path: null, pathSegments: ['']);
  Uri(path: path, pathSegments: ['']);
  Uri(queryParameters: {'empty': null});
  Uri(queryParameters: {'empty': <String>[]});
  Uri(queryParameters: {'values': <String>{'first', 'second'}});
  Uri(queryParameters: {'values': values});
  Uri(queryParameters: {'unknown': unknown});
  core.Uri(queryParameters: {'hello': 'world'});
  Uri(query: 'key=value');
  Uri(queryParameters: {'key': 'value'});
  Uri(query: null, queryParameters: {'key': 'value'});
  Uri(query: 'key=value', queryParameters: null);
  Uri(query: path, queryParameters: {'key': 'value'});
  core.Uri(query: '', queryParameters: <String, String>{});
  Uri(query: (r''), queryParameters: {'key': 'value'});
}

void shadowed(dynamic Uri) {
  Uri(queryParameters: {'count': 1});
  Uri.https('', '', {'count': 1});
}

class CustomUri {
  CustomUri({dynamic queryParameters});
}
void other() {
  CustomUri(queryParameters: {'count': 1});
}

void collections(bool includeCount, List<String> values) {
  Uri(queryParameters: {
    if (includeCount) 'count': '1' else 'other': null,
    for (final value in values) value: value,
    for (final value in values)
      if (includeCount) 'nested$value': <String>['valid'],
  });
}

Configure avoid-incorrect-uri

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-incorrect-uri": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/suspicious/avoid-incorrect-uri: <reason>