简体   繁体   English

为什么dart分析仪无法识别类型错误?

[英]Why dart analyzer fails to recognize a type error?

Here's the code:这是代码:

static const _defaults = <String, dynamic>{
  'api_key': '4839234792374',
  'enabled': false,
  'concurrency': 4,
};

String getString(String key) {
  return _remoteConfig == null ?
    _defaults.containsKey(key) && _defaults[key] :
    _remoteConfig.getString(key);
}

The bug is obvious (and shame on me, was produced by blind copy-paste from similar getBool(key) function. If the _remoteConfig is null , the execution hits the bool && String path and I get the runtime exception type 'String' is not a subtype of type 'bool' . Totally legit, but why does analyzer not see it? The execution flow is crystal clear, one path returns String another path (theoretically) returns dynamic and the return type is String which means that all paths return String . What I don't understand?该错误很明显(令我感到羞耻,是由类似的getBool(key) function 的盲目复制粘贴产生的。如果_remoteConfignull ,则执行命中bool && String路径,我得到运行时异常type 'String' is not a subtype of type 'bool' 。完全合法,但为什么分析器看不到它?执行流程非常清晰,一条路径返回String ,另一条路径(理论上)返回dynamic ,返回类型为String ,这意味着所有路径都返回String . 什么我不明白?

Dart 2.12.0 pedantic 1.11.0 Dart 2.12.0 迂腐 1.11.0

Analyzer options:分析仪选项:

include: package:pedantic/analysis_options.yaml

analyzer:
  exclude:
    - lib/generated/*
    - lib/**/*.g.dar

I think you're asking two questions:我想你在问两个问题:

  1. Why doesn't the analyzer complain about _defaults.containsKey(key) && _defaults[key] ?为什么分析器不抱怨_defaults.containsKey(key) && _defaults[key]

  2. Why doesn't the analyzer complain about the ternary expression with different types along its two paths?为什么分析器不抱怨沿其两条路径具有不同类型的三元表达式?

For #1: Since _defaults[key] returns type dynamic (which could be a bool at runtime), I wouldn't expect an analysis complaint.对于#1:由于_defaults[key]返回类型dynamic (在运行时可能是bool ),我不希望分析投诉。

For #2: Since the two paths have different types, the type of the ternary expression is the common base type : Object .对于#2:由于两个路径具有不同的类型,因此三元表达式的类型是公共基本类型Object If implicit casts are enabled, Object is then automatically cast to the String return type.如果启用了隐式转换,则Object会自动转换为String返回类型。

The analyzer does catch both errors if you disable implicit casts in your analysis_options.yaml configuration file:如果您在analysis_options.yaml配置文件中禁用隐式转换,分析器捕获这两个错误:

analyzer:
  strong-mode:
    implicit-casts: false

Running the analyzer then prints:运行分析器然后打印:

error • The operands of the operator '&&' must be assignable to 'bool' at... • (non_bool_operand)错误 • 运算符 '&&' 的操作数必须可在...处分配给 'bool' • (non_bool_operand)

error • A value of type 'Object' can't be returned from the function 'getString' because it has a return type of 'String' at... • (return_of_invalid_type)错误 • 无法从 function 的“getString”返回类型为“Object”的值,因为它的返回类型为“String”... • (return_of_invalid_type)

(I'm surprised that implicit-casts: false triggers an analysis error for the && expression with a dynamic operand,; maybe my understanding isn't quite accurate, or maybe that's a bug in the analyzer.) (我很惊讶implicit-casts: false会触发带有dynamic操作数的&&表达式的分析错误;也许我的理解不太准确,或者这可能是分析器中的错误。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM