Bug 385472 - Incorrect indentation in nested 'if' statements without braces
Summary: Incorrect indentation in nested 'if' statements without braces
Status: RESOLVED FIXED
Alias: None
Product: frameworks-ktexteditor
Classification: Frameworks and Libraries
Component: general (show other bugs)
Version: 5.58.0
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-10-07 18:09 UTC by Алексей Шилин
Modified: 2022-01-24 07:14 UTC (History)
4 users (show)

See Also:
Latest Commit:
Version Fixed In: 5.91


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Алексей Шилин 2017-10-07 18:09:53 UTC
Hi,

In the following C code the last 'else' is indented incorrectly:

int f()
{
    if (1)
        if (1)
            f();
        else
            f();
        else         <-- Should be one indentation level lower

The same is true for JavaScript (and hence QML):

function f() {
    if (true)
        if (true)
            f();
        else
            f();
        else

Other languages were not tested.
Comment 1 Edward Kigwana 2019-05-25 15:03:59 UTC
With braces it works as expected.

int f() {
	if (1) {
		if (1)
			f();
		else
			f();
	}  else {
		f();
	}
}
Comment 2 Karthik Periagaram 2019-05-26 06:18:30 UTC
Confirmed using latest released packages. Updating version to indicate.

Tested using Arch linux packages (KDE frameworks 5.58.0, Qt 5.12.3, Kwrite 19.04.1).
Comment 3 Ahmad Samir 2019-07-02 13:20:42 UTC
The first "if" must use curly braces because the nested if is intended to be executed as a block, IIUC... (mind you, I only know C++, not C).
Comment 4 Karthik Periagaram 2019-07-11 05:07:23 UTC
(In reply to Ahmad Samir from comment #3)
> The first "if" must use curly braces because the nested if is intended to be
> executed as a block, IIUC... (mind you, I only know C++, not C).

That is not correct. The nested conditional construct without explicitly marking off the block scope with {} is valid in both C and C++.
Comment 5 Bug Janitor Service 2022-01-18 12:13:04 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/259
Comment 6 Bug Janitor Service 2022-01-23 13:09:37 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/266
Comment 7 Waqar Ahmed 2022-01-23 19:18:44 UTC
Fixed with 5.91
Comment 8 Christoph Cullmann 2022-01-24 07:14:08 UTC
Git commit f1b94f2f8367a07e4ed4b8e33f6061383b3aa44f by Christoph Cullmann, on behalf of Waqar Ahmed.
Committed on 23/01/2022 at 18:31.
Pushed by waqar into branch 'master'.

Handle single statement condition blocks better

See test case for full detail of what is fixed. Essentially it is
that if you have something like:

```
    if (true)
        doWork();
```

and the cursor is on the semicolon, pressing enter should decrease
indent level to that of the `if` statement.

A  +8    -0    autotests/input/indent/cstyle/385472/expected
A  +3    -0    autotests/input/indent/cstyle/385472/input.js
A  +7    -0    autotests/input/indent/cstyle/385472/origin
M  +34   -1    src/script/data/indentation/cstyle.js

https://invent.kde.org/frameworks/ktexteditor/commit/f1b94f2f8367a07e4ed4b8e33f6061383b3aa44f