Questions tagged [unreachable-code]

Unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.

181 questions
10
votes
5 answers

C# Unreachable code detected

I'm getting a "Unreachable code detected" message in Visual Studio at the point i++ in my code below. Can you spot what I've done wrong? try { RegistryKey OurKey = Registry.CurrentUser; OurKey.CreateSubKey("Software\\Resources\\Shared"); …
Jamie
  • 2,465
  • 10
  • 28
  • 31
9
votes
2 answers

Why does Rust allow code with the wrong return type, but only with a trailing semicolon?

Consider the following Rust code: fn f() -> i32 { loop { println!("Infinite loop!"); } println!("Unreachable"); } This compiles (with a warning) and runs, despite the fact that the return type is wrong. It would seem that the…
Caleb Stanford
  • 453
  • 2
  • 6
  • 15
9
votes
2 answers

javascript switch break unreachable code detected

I have the following code switch (attr.templateType) { case 'text': return tpl_default; break; case 'currency': return tpl_currency; break; case 'percentage': return tpl_percentage; break; case 'latlong': return…
user2062455
  • 411
  • 5
  • 14
8
votes
3 answers

Why java does not detect unreachable catch block if I use multiple catch blocks?

Research following method: static private void foo() { try { throw new FileNotFoundException(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
8
votes
1 answer

Avoid "Unreachable code" warning for preprocessor-dependent code

I'm trying to figure out if there's any way to avoid getting an "Unreachable code" warning for something that's caused by the preprocessor. I don't want to suppress all such warnings, only those which will be dependent on the preprocessor, e.g. #if…
7
votes
2 answers

Ada suppress unreachable code or missing return

I have a tagged type that implements a number of functions. In one case I need one of these functions to instead enter an infinite loop. Unfortunately as far as I can tell there is no way for me to compile this such that it doesn't raise a warning.…
LambdaBeta
  • 1,479
  • 1
  • 13
  • 25
7
votes
6 answers

How to handle "not all code paths return a value" when the logic of the function does ensure a return

I suppose the easiest way to explain is by a contrived example: public static int Fail() { var b = true; if (b) { return 0; } } This code will not compile, and gives the error "not all code paths return a value," while we humans…
Brent
  • 4,153
  • 4
  • 30
  • 63
7
votes
4 answers

Unreachable code at constructor's closing brace

I'm working on an application built with VC9 and I've hit upon a warning I don't fully understand: why is there an "unreachable code" warning on the closing brace of the constructor? The minimal testcase to reproduce the issue…
mrkj
  • 3,041
  • 19
  • 24
6
votes
2 answers

Why unreachable code isn't an error in C++?

unreachable code is compile time error in languages like Java. But why it is just warning in C++ & C? Consider following example: #include int f() { int a=3; return a; int b=6; // oops it is unreachable code …
Destructor
  • 14,123
  • 11
  • 61
  • 126
5
votes
3 answers

"Unreachable code detected" in MSIL or Native code

Does compiler compile "Unreachable Codes" in MSIL or Native code in run time?
Babak
  • 3,716
  • 6
  • 39
  • 56
5
votes
1 answer

Why is gcc emitting worse code with __builtin_unreachable?

With f0 and f1 as below, long long b; void f0(int a) { a %= 10; if (a == 0) b += 11; else if (a == 1) b += 13; else if (a == 2) b += 17; else if (a == 3) b += 19; else if (a == 4) b += 23; else if (a == 5) b += 29; …
user3810155
5
votes
0 answers

Why does a properly functioning Rust library fail with unreachable code error when compiled to WASM using wasm-bindgen?

Consider this library as a dependency: [dependencies] rustengine = "1.0.60" Compile this tiny program against it: use rustenginelib::uci::*; fn main() { let mut uci = create_default_uci(); uci.process_uci_command("perft…
5
votes
3 answers

When does the condition "Unreachable Code" occur in Java?

When there is some statement written after the infinite loop, that statement becomes the unreachable code. For ex: for(;;) { } Sytem.out.println("Test-1"); //unreachable code But I am facing some difficulty here. Look at the two code snippets…
Vikas Mangal
  • 821
  • 3
  • 10
  • 23
5
votes
4 answers

Unreachable code detected by using const variables

I have following code: private const FlyCapture2Managed.PixelFormat f7PF = FlyCapture2Managed.PixelFormat.PixelFormatMono16; public PGRCamera(ExamForm input, bool red, int flags, int drawWidth, int drawHeight) { if (f7PF ==…
SinisterMJ
  • 3,425
  • 2
  • 33
  • 53
5
votes
3 answers

Visual C++ error: function must return a value

I am working on a multiplatform project, and some platforms have disabled features, and in the interface for those features, a common thing I do is something like this: bool Foo::bar() const { // disabled abort(); } GCC/LLVM do not require…
fbafelipe
  • 4,862
  • 2
  • 25
  • 40
1
2
3
12 13