Eclipse继续说明最后的elseif和else是死代码,但我不明白.
if (img0 != null && img1 != null) {
code;
} else if (img0 != null) {
code;
} else if (img1 != null) {
code;
} else {
code;
}
Run Code Online (Sandbox Code Playgroud)
我的理由是这样的:
我错过了什么,"死亡"在哪里?
提前致谢.
我担心我的应用程序中的某些类具有已定义但未在应用程序中的任何位置调用的方法.
在Eclipse中有没有办法找到这些方法?
我不明白为什么Eclipse在第二个if条件中给出了代码的死代码警告:
boolean frameErreicht = false;
while (!frameErreicht) {
String line = reader.readLine();
if (line.matches("@\\d*")) {
reader.mark(reader.getLineNumber() - 1);
reader.setLineNumber(reader.getLineNumber() - 1);
frameErreicht = true;
}
if (line == null)
throw new IOException("Keine Angaben zu Frames im Eingabestrom");
}
Run Code Online (Sandbox Code Playgroud)
LinenumberReader的readLine()方法的jdoc表示null如果到达流的末尾它将返回,所以如果在整个text(line == null)中找不到匹配,它应该抛出异常.
但是怎么了?
我正在研究嵌入式系统。我用C来编程,用ARM编译器来编译。
我正在编写的代码已经存在一段时间了,并且是由多人开发的。它也使用函数指针。
有没有任何工具可以帮助我检测死代码?如果没有,你建议我如何去做?
通过我的一个队友写的一些旧代码,我发现这个奇怪的代码:
if (...) {
// some code
} else if (this == null) {
System.out.println("I expected this to be dead code!");
}
Run Code Online (Sandbox Code Playgroud)
奇怪的不是它.AFAIK,this == null条件永远不会true,编译器应该是显而易见的,因为它知道this和null两者的含义.但令我惊讶的是,这并没有标记为死代码.
我在Eclipse中通过命令行尝试了这段代码.我运行以下命令来启用所有警告:
javac -Xlint:all MyClass.java
Run Code Online (Sandbox Code Playgroud)
它还没有发出任何警告.
相反,如果我将else if块更改为:
else if (false) {
System.out.println("As expected, this is dead code");
}
Run Code Online (Sandbox Code Playgroud)
正如我所料,内部声明被标记为死代码.
那为什么会这样呢?这只会导致我认为可能有一些条件,其中this居然能null.是吗?
我们刚刚在我们的代码库中发现了一个问题,即语句在 return 语句之后。
例如
std::string MyClass::addElement(Type1 &item, const std::string ¶m2)
{
if (param2.empty())
{
// logging
return "";
}
return m_database->addElement(item, param2, item.status, true);
// here I would expect an unreachable code warning
m_database->updateTableA(item.p1, item.p2, item.p3, AType::AType23);
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是,为什么我们的编译器(GCC 4.8.5 和 7)不发出警告?
我们编译 -std=c++0x -Wall -Wextra -Wsign-compare -Wunknown-pragmas -Wold-style-cast -Wshadow -Wfatal-errors
我想编写一个函数,根据用户设置,可以执行或不执行某些可选代码.该函数是cpu密集型的,并且if中的ifs会很慢,因为分支预测器不是那么好.
我的想法是在函数的内存中复制并在我不想执行某些代码时用跳转替换NOP.我的工作示例如下:
int Test()
{
int x = 2;
for (int i=0 ; i<10 ; i++)
{
x *= 2;
__asm {NOP}; // to skip it replace this
__asm {NOP}; // by JMP 2 (after the goto)
x *= 2; // Op to skip or not
x *= 2;
}
return x;
}
Run Code Online (Sandbox Code Playgroud)
在我的测试主程序中,我将此函数复制到新分配的可执行内存中,并用JMP 2替换NOP,以便不执行以下x*= 2.JMP 2实际上是"跳过接下来的2个字节".
问题是每次编辑要跳过的代码并更改其大小时,我都必须更改JMP操作数.
解决这个问题的另一种方法是:
__asm {NOP}; // to skip it replace this
__asm {NOP}; // by JMP 2 (after the goto)
goto dont_do_it;
x *= …Run Code Online (Sandbox Code Playgroud) 如果在C++中没有使用某些东西,它根本就没有编译.iPhone也是如此吗?如果我编译一个程序并且有未使用的类或其他东西,它会被编译或忽略吗?
在学习 Rust 并尝试 Rust 书中的示例单元测试相关代码时:https://doc.rust-lang.org/book/ch11-01-writing-tests.html
我收到一条关于死代码的警告,这些代码显然正在由单元测试执行。这是为什么?
lib.rs 中的代码
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn larger_can_hold_smaller() {
let larger = Rectangle {
width: 8,
height: 7,
};
let smaller = Rectangle {
width: 5,
height: 1,
};
assert!(larger.can_hold(&smaller));
}
}
Run Code Online (Sandbox Code Playgroud)
运行货物测试时的结果
$ cargo test
Compiling adder v0.1.0 (/Users/khorkrak/projects/rust/adder)
warning: associated function …Run Code Online (Sandbox Code Playgroud) 我有一些我正在为一堂课写的VHDL代码.但是,综合工具将cell3,cell2和cell1识别为"死"代码,并且不会合成它.
我真的不知道是什么导致细胞3,2,1在合成中被去除; 我已经回顾了5次以上并且询问了几个不同的人,我找不到"为什么".
不寻找解决方案,只是指向原因的指针.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity multiply is
Port ( a : in STD_LOGIC_VECTOR (3 downto 0);
b : in STD_LOGIC;
clk : in STD_LOGIC;
rst : in STD_LOGIC;
p : out STD_LOGIC);
end multiply;
architecture Behavioral of multiply is
component cell_a port(
s: in std_logic;
c: in std_logic;
a: in std_logic;
b: in std_logic;
clk: in std_logic;
c_out: out std_logic;
s_out: out std_logic);
end component;
signal c_s_0: std_logic; --loopback wire for cell …Run Code Online (Sandbox Code Playgroud) dead-code ×10
java ×4
eclipse ×3
c++ ×2
assembly ×1
c ×1
embedded ×1
function ×1
gcc ×1
if-statement ×1
iphone ×1
objective-c ×1
pointers ×1
refactoring ×1
rust ×1
synthesis ×1
this ×1
unit-testing ×1
vhdl ×1