我试图检查用户给出的单词是否已经存在于文本文件中,或者它的子字符串是否已存在.这是我的代码:
String ans = null;
Scanner scanner = null;
do
{
System.out.print("Please enter a new word: ");
String Nword = scan.next();
System.out.print("And its appropriate Hint: ");
String Nhint = scan.next();
Word word = new Word(Nword , Nhint);
File file = new File("C:\\Users\\Charbel\\Desktop\\Dictionary.txt");
file.createNewFile();
scanner = new Scanner(file);
if (scanner != null)
{
String line;
while (scanner.hasNext()) {
line = scanner.next();
for(int i = 0 ; i<line.length(); i++)
if ((line.equals(Nword)) || (Nword.equals(line.substring(i))))
{
System.out.println("The word already exists.");
break;
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Eclipse 4.3 Kepler(实际上是STS 3.6.1).
我碰到了一些代码:
private String someMethod(String myParam) {
try {
MyInterface myVar = (MyInterface) domeSomething(myParam);
if (myVar != null) {
return myVar.methodThatReturnsString();
}
} catch (Exception e) {
return "";
}
return ""; // eclipse marks this as dead code
}
Run Code Online (Sandbox Code Playgroud)
(正如您所期望的那样,该doSomething()方法会抛出一些异常,并返回一个比一般更通用的接口MyInterface.)
Eclipse将最后一个return语句强调为死代码,如果我将其删除为quickfix建议,我将使用"此方法应返回String类型的结果"错误.
为什么最后一个return语句是死代码?是因为班级演员吗?假设doSomething()可以返回null,如果你施放它,会抛出一个类强制转换异常吗?
而且,为什么Eclipse建议我用导致死代码警告的错误修复错误?是因为Eclipse无法预测这个吗?
我在使用Eclipse的Java代码片段中遇到了死代码错误:
public void rebirthAction() {
Player p = new Player(null);
Equipment e = new Equipment();
Skills s = new Skills(null);
if ((Equipment.SLOT_SHIELD == -1) && (Equipment.SLOT_WEAPON == -1) && (Equipment.SLOT_CHEST == -1) && (Equipment.SLOT_BOTTOMS == -1) && (Equipment.SLOT_AMULET == -1) && (Equipment.SLOT_BOOTS == -1) && (Equipment.SLOT_HELM == -1) && (Equipment.SLOT_GLOVES == -1))
for (int i = 0; i <= 7; i++) {
p.getSkills().setLevel(i, 1);
p.getSkills().setExperience(i, 0);
//updateRequired = true;
//appearanceUpdateRequired = true;
s.getTotalLevel();
s.getCombatLevel();
Combat.calculateMaxHit(p);
p.getSkills();
rebirthCount++;
}
}
Run Code Online (Sandbox Code Playgroud) 我想分析我的projet,xcode分析器在我的root控制器中找到一些死代码Xcode告诉我:
Dead code
The left operand to '+' is always 0
-> Variable 'i' initialized to 0
-> The left operand to '+' is always 0
Run Code Online (Sandbox Code Playgroud)
有人可以解释我这个如何清理该代码,谢谢....
这是我的rootcontroller.m
#import "RootViewController22.h"
#import "LabelCell.h"
#import "NibLoadedCell.h"
#import "GradientBackgroundTable.h"
#import "NibLoadedCell2.h"
#import "NibLoadedCell3.h"
@implementation RootViewController22
//
// title
//
// returns the navigation bar text for the front screen
//
/*- (NSString *)title
{
return NSLocalizedString(@"Les Robes du Bengal", @"");
}*/
//
// createRows
//
// Constructs all the rows on the front …Run Code Online (Sandbox Code Playgroud) 虽然在eclipse中找到 "死代码"线程,但这里 死代码警告
我尝试了以下简单的java代码:
public class Test2
{
public static void main(String[] args)
{
int x = 0;
while(false)
{
x=4;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这正确地抛出了编译时错误
C:\Documents and Settings\user\Desktop\Test2.java:7: unreachable
statement
{
^ 1 error
Run Code Online (Sandbox Code Playgroud)
我稍微调整了一下代码:
public class Test2
{
public static void main(String[] args)
{
int x =0;
while(true)
{
x=4;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它编译得很好.
有没有理由,为什么编译好?
逻辑上两者都应该导致infiniteloop并且都应该导致编译时错误.
难道我做错了什么?
while(true){
try
{
if(Calendar.DATE == X){
startTask();
}
long delay = timeUntilNextCheck();
Thread.sleep(delay);
}
catch (Throwable t)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个程序,需要在一个月的特定日期运行特定任务.在任务运行之后(或者如果它不是那天),线程会一直睡到明天它将再次检查.
但是,我收到了死代码警告,因为部分代码仅在该月的特定日期运行.
我已经阅读了这个警告是什么,我发现在某些情况下编译器不会编译死代码.所以我的问题是,这总是会被编译吗?
关于以下片段的一些解释:
我正在搞乱一些蓝牙发现电话.为此,我正在使用回调,如果BluetoothDevice找到a,将调用该回调.如果未找到设备,则参数为null:
@Override
public void provideDevice(BluetoothDevice device) {
super.provideDevice(device);
Log.v("MainActivity","device name = " +device.getName());
if(device != null) {
mBinder.start(device);
} else {
Toast.makeText(this, "No Device found", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
Eclipse告诉我else块是死代码.
如果我Log在if-block中移动调用,则警告消失:
@Override
public void provideDevice(BluetoothDevice device) {
super.provideDevice(device);
if(device != null) {
Log.v("MainActivity","device name = " +device.getName());
mBinder.start(bc);
} else {
Toast.makeText(this, "No Device found", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道如果参数为null,第一个片段将抛出NPE.这不是本例中的问题.
我想知道为什么会dead code warning出现.
我可以提供完整的代码,如果这还不足以告诉我发生了什么.
我有一个对象数组.我想要扫描它,只要我找到的对象不为空,就将计数器增加1.当我找到第一个空对象时,我想要从for循环中取出jumb,因为没有理由继续循环.
我写了以下代码:
// counter variable initialized to zero
int counter = 0;
// scan the array
for(int i = 0; i < this.array.length; i++) {
// as long as the object found is not null
while(!this.array[i].equals(null)) {
// increase the value of the counter by 1
counter += 1;
}
// when the first null object found, jump out of the loop
break;
}
Run Code Online (Sandbox Code Playgroud)
for循环中的i ++被标记,警告是Dead Code.但是,我想这是有道理的,因为当我找到第一个空对象时,我停止循环.所以没什么可担心的,或者......?
使用这个(简化的)代码示例Eclipse(Kepler SR2)为最内层的if语句(if (con != null)),死代码发出警告.
public class DbManager {
public String getSingleString(String query) throws SQLException {
DbManager dbmgr = new DbManager();
Connection con = null;
try {
con = dbmgr.getConnection("user", "pwd", URL);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
PreparedStatement pstmt = null;
ResultSet rset = null;
pstmt = con.prepareStatement(query.toString());
rset = pstmt.executeQuery();
if (rset != null && rset.next()) {
return (rset.getString(1));
}
}
}
return …Run Code Online (Sandbox Code Playgroud) 当一个函数只从测试中调用时,rust 会抱怨它从未被使用过。为什么会发生这种情况以及如何解决这个问题?
例子:
fn greet() {
println!("Hello!")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_greet() {
greet();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下编译器警告:
Compiling playground v0.0.1 (/playground)
warning: function is never used: `greet`
--> src/lib.rs:1:4
|
1 | fn greet() {
| ^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: 1 warning emitted
Run Code Online (Sandbox Code Playgroud)