Skip to content

Check for shadowing in lfs.c and ensure clean compilation#1150

Open
DarukaThirumurugan wants to merge 1 commit intolittlefs-project:masterfrom
DarukaThirumurugan:master
Open

Check for shadowing in lfs.c and ensure clean compilation#1150
DarukaThirumurugan wants to merge 1 commit intolittlefs-project:masterfrom
DarukaThirumurugan:master

Conversation

@DarukaThirumurugan
Copy link
Copy Markdown

@DarukaThirumurugan DarukaThirumurugan commented Oct 9, 2025

Refactor: cleared variable shadowing in lfs.c

Identified and resolved instances of variable shadowing in lfs.c.
This improves code readability and reduces the risk of bugs
due to accidental re-use of variable names in inner scopes.

Used -Wshadow flag to detect issues and verified successful
compilation with no warnings.

In the original code there are many shadows occured as a warning

Screenshot 2025-10-14 193747

After Altering

Screenshot 2025-10-14 193329

There is no shadow occurs

Copy link
Copy Markdown

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of the introduced variables doesn't really improve clarity per se.

A better approach would be checking which of these cases may actually allow to skip these additional variables and use the outer scoped one (safely). That way this would even improve on stack frame size.

Nothing against this PR (I'm all for enforcing -Wshadow, it's just that this currently doesn't look clean either … So more refinement might be good.

Comment thread test_main.c
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated new test, that doesn't actually test anything in LittleFS.

Comment thread lfs.c
Comment on lines +4809 to +4810
err1 = lfs_tortoise_detectcycles(pdir, &tortoise);
if (err1 < 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inlining this check would eliminate the need for err1 entirely AFAICS.

Suggested change
err1 = lfs_tortoise_detectcycles(pdir, &tortoise);
if (err1 < 0) {
if (lfs_tortoise_detectcycles(pdir, &tortoise) < 0) {

Comment thread lfs.c
#include "lfs_util.h"



Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated white space change.

Comment thread lfs.c
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
f->ctz.head, f->ctz.size, cb, data);
if (err) {
if (err_traverse ) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduces white space/formatting change.

Suggested change
if (err_traverse ) {
if (err_traverse) {

Comment thread lfs.c
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
f->ctz.head, f->ctz.size, cb, data);
if (err) {
if (err_traverse ) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduces white space/formatting change.

Suggested change
if (err_traverse ) {
if (err_traverse) {

Comment thread lfs.c

if ((f->flags & LFS_F_WRITING) && !(f->flags & LFS_F_INLINE)) {
int err = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduces white space/formatting change.

Suggested change
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,

Comment thread lfs.c

if ((f->flags & LFS_F_WRITING) && !(f->flags & LFS_F_INLINE)) {
int err = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduces white space/formatting change.

Suggested change
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
int err_traverse = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,

Comment thread lfs.c
Comment on lines +4786 to +4787
if (err_traverse ) {
return err_traverse ;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduces white space/formatting change.

Suggested change
if (err_traverse ) {
return err_traverse ;
if (err_traverse) {
return err_traverse;

@geky geky added the lint label Oct 9, 2025
@geky
Copy link
Copy Markdown
Member

geky commented Oct 9, 2025

Hi @DarukaThirumurugan, I appreciate the pull request, but IMO this is an unproductive warning and does more harm than good. See #873 and #873 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants