Skip to content

Commit 4ab3050

Browse files
authored
Rollup merge of #85124 - jsha:trust-the-bool, r=GuillaumeGomez
rustdoc: remove explicit boolean comparisons. For boolean variables it's shorter and more readable to check the value directly, or negate it with `!`. In a couple of cases I reordered an if/else pair because it made the initial `if` statement simpler. An example of a style guide recommending this: https://airbnb.io/javascript/#comparison--shortcuts r? `@GuillaumeGomez`
2 parents 649b385 + f510e41 commit 4ab3050

File tree

4 files changed

+64
-69
lines changed

4 files changed

+64
-69
lines changed

src/librustdoc/html/static/main.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,14 @@ function hideThemeButtonState() {
448448
}
449449

450450
function getHelpElement(build) {
451-
if (build !== false) {
451+
if (build) {
452452
buildHelperPopup();
453453
}
454454
return document.getElementById("help");
455455
}
456456

457457
function displayHelp(display, ev, help) {
458-
if (display === true) {
458+
if (display) {
459459
help = help ? help : getHelpElement(true);
460460
if (hasClass(help, "hidden")) {
461461
ev.preventDefault();
@@ -466,7 +466,7 @@ function hideThemeButtonState() {
466466
// No need to build the help popup if we want to hide it in case it hasn't been
467467
// built yet...
468468
help = help ? help : getHelpElement(false);
469-
if (help && hasClass(help, "hidden") === false) {
469+
if (help && !hasClass(help, "hidden")) {
470470
ev.preventDefault();
471471
addClass(help, "hidden");
472472
removeClass(document.body, "blur");
@@ -477,9 +477,9 @@ function hideThemeButtonState() {
477477
function handleEscape(ev) {
478478
var help = getHelpElement(false);
479479
var search = searchState.outputElement();
480-
if (hasClass(help, "hidden") === false) {
480+
if (!hasClass(help, "hidden")) {
481481
displayHelp(false, ev, help);
482-
} else if (hasClass(search, "hidden") === false) {
482+
} else if (!hasClass(search, "hidden")) {
483483
searchState.clearInputTimeout();
484484
ev.preventDefault();
485485
searchState.hideResults(search);
@@ -491,7 +491,7 @@ function hideThemeButtonState() {
491491
var disableShortcuts = getSettingValue("disable-shortcuts") === "true";
492492
function handleShortcut(ev) {
493493
// Don't interfere with browser shortcuts
494-
if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts === true) {
494+
if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts) {
495495
return;
496496
}
497497

@@ -908,11 +908,11 @@ function hideThemeButtonState() {
908908
function implHider(addOrRemove, fullHide) {
909909
return function(n) {
910910
var shouldHide =
911-
fullHide === true ||
912-
hasClass(n, "method") === true ||
913-
hasClass(n, "associatedconstant") === true;
914-
if (shouldHide === true || hasClass(n, "type") === true) {
915-
if (shouldHide === true) {
911+
fullHide ||
912+
hasClass(n, "method") ||
913+
hasClass(n, "associatedconstant");
914+
if (shouldHide || hasClass(n, "type")) {
915+
if (shouldHide) {
916916
if (addOrRemove) {
917917
addClass(n, "hidden-by-impl-hider");
918918
} else {
@@ -934,7 +934,7 @@ function hideThemeButtonState() {
934934

935935
var relatedDoc;
936936
var action = mode;
937-
if (hasClass(toggle.parentNode, "impl") === false) {
937+
if (!hasClass(toggle.parentNode, "impl")) {
938938
relatedDoc = toggle.parentNode.nextElementSibling;
939939
if (hasClass(relatedDoc, "item-info")) {
940940
relatedDoc = relatedDoc.nextElementSibling;
@@ -964,11 +964,11 @@ function hideThemeButtonState() {
964964
relatedDoc = parentElem;
965965
var docblock = relatedDoc.nextElementSibling;
966966

967-
while (hasClass(relatedDoc, "impl-items") === false) {
967+
while (!hasClass(relatedDoc, "impl-items")) {
968968
relatedDoc = relatedDoc.nextElementSibling;
969969
}
970970

971-
if (!relatedDoc && hasClass(docblock, "docblock") === false) {
971+
if (!relatedDoc && !hasClass(docblock, "docblock")) {
972972
return;
973973
}
974974

@@ -987,7 +987,7 @@ function hideThemeButtonState() {
987987
if (action === "show") {
988988
removeClass(relatedDoc, "fns-now-collapsed");
989989
// Stability/deprecation/portability information is never hidden.
990-
if (hasClass(docblock, "item-info") === false) {
990+
if (!hasClass(docblock, "item-info")) {
991991
removeClass(docblock, "hidden-by-usual-hider");
992992
}
993993
onEachLazy(toggle.childNodes, adjustToggle(false, dontApplyBlockRule));
@@ -996,7 +996,7 @@ function hideThemeButtonState() {
996996
addClass(relatedDoc, "fns-now-collapsed");
997997
// Stability/deprecation/portability information should be shown even when detailed
998998
// info is hidden.
999-
if (hasClass(docblock, "item-info") === false) {
999+
if (!hasClass(docblock, "item-info")) {
10001000
addClass(docblock, "hidden-by-usual-hider");
10011001
}
10021002
onEachLazy(toggle.childNodes, adjustToggle(true, dontApplyBlockRule));
@@ -1045,7 +1045,7 @@ function hideThemeButtonState() {
10451045
});
10461046
}
10471047

1048-
if (hideMethodDocs === true) {
1048+
if (hideMethodDocs) {
10491049
onEachLazy(document.getElementsByClassName("method"), function(e) {
10501050
var toggle = e.parentNode;
10511051
if (toggle) {
@@ -1132,7 +1132,7 @@ function hideThemeButtonState() {
11321132
if (sidebar_menu) {
11331133
sidebar_menu.onclick = function() {
11341134
var sidebar = document.getElementsByClassName("sidebar")[0];
1135-
if (hasClass(sidebar, "mobile") === true) {
1135+
if (hasClass(sidebar, "mobile")) {
11361136
hideSidebar();
11371137
} else {
11381138
showSidebar();

src/librustdoc/html/static/search.js

+35-39
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,21 @@ window.initSearch = function(rawSearchIndex) {
146146

147147
removeEmptyStringsFromArray(split);
148148

149-
function transformResults(results, isType) {
149+
function transformResults(results) {
150150
var out = [];
151151
for (var i = 0, len = results.length; i < len; ++i) {
152152
if (results[i].id > -1) {
153153
var obj = searchIndex[results[i].id];
154154
obj.lev = results[i].lev;
155-
if (isType !== true || obj.type) {
156-
var res = buildHrefAndPath(obj);
157-
obj.displayPath = pathSplitter(res[0]);
158-
obj.fullPath = obj.displayPath + obj.name;
159-
// To be sure than it some items aren't considered as duplicate.
160-
obj.fullPath += "|" + obj.ty;
161-
obj.href = res[1];
162-
out.push(obj);
163-
if (out.length >= MAX_RESULTS) {
164-
break;
165-
}
155+
var res = buildHrefAndPath(obj);
156+
obj.displayPath = pathSplitter(res[0]);
157+
obj.fullPath = obj.displayPath + obj.name;
158+
// To be sure than it some items aren't considered as duplicate.
159+
obj.fullPath += "|" + obj.ty;
160+
obj.href = res[1];
161+
out.push(obj);
162+
if (out.length >= MAX_RESULTS) {
163+
break;
166164
}
167165
}
168166
}
@@ -266,9 +264,7 @@ window.initSearch = function(rawSearchIndex) {
266264
path = result.item.path.toLowerCase(),
267265
parent = result.item.parent;
268266

269-
if (isType !== true &&
270-
validateResult(name, path, split, parent) === false)
271-
{
267+
if (!isType && !validateResult(name, path, split, parent)) {
272268
result.id = -1;
273269
}
274270
}
@@ -352,7 +348,7 @@ window.initSearch = function(rawSearchIndex) {
352348
var lev_distance = MAX_LEV_DISTANCE + 1;
353349
var len, x, firstGeneric;
354350
if (obj[NAME] === val.name) {
355-
if (literalSearch === true) {
351+
if (literalSearch) {
356352
if (val.generics && val.generics.length !== 0) {
357353
if (obj.length > GENERICS_DATA &&
358354
obj[GENERICS_DATA].length >= val.generics.length) {
@@ -373,7 +369,7 @@ window.initSearch = function(rawSearchIndex) {
373369
break;
374370
}
375371
}
376-
if (allFound === true) {
372+
if (allFound) {
377373
return true;
378374
}
379375
} else {
@@ -394,7 +390,7 @@ window.initSearch = function(rawSearchIndex) {
394390
}
395391
}
396392
// Names didn't match so let's check if one of the generic types could.
397-
if (literalSearch === true) {
393+
if (literalSearch) {
398394
if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
399395
return obj[GENERICS_DATA].some(
400396
function(name) {
@@ -429,12 +425,12 @@ window.initSearch = function(rawSearchIndex) {
429425
var length = obj.type[INPUTS_DATA].length;
430426
for (var i = 0; i < length; i++) {
431427
var tmp = obj.type[INPUTS_DATA][i];
432-
if (typePassesFilter(typeFilter, tmp[1]) === false) {
428+
if (!typePassesFilter(typeFilter, tmp[1])) {
433429
continue;
434430
}
435431
tmp = checkType(tmp, val, literalSearch);
436-
if (literalSearch === true) {
437-
if (tmp === true) {
432+
if (literalSearch) {
433+
if (tmp) {
438434
return true;
439435
}
440436
continue;
@@ -445,7 +441,7 @@ window.initSearch = function(rawSearchIndex) {
445441
}
446442
}
447443
}
448-
return literalSearch === true ? false : lev_distance;
444+
return literalSearch ? false : lev_distance;
449445
}
450446

451447
function checkReturned(obj, val, literalSearch, typeFilter) {
@@ -458,12 +454,12 @@ window.initSearch = function(rawSearchIndex) {
458454
}
459455
for (var x = 0, len = ret.length; x < len; ++x) {
460456
var tmp = ret[x];
461-
if (typePassesFilter(typeFilter, tmp[1]) === false) {
457+
if (!typePassesFilter(typeFilter, tmp[1])) {
462458
continue;
463459
}
464460
tmp = checkType(tmp, val, literalSearch);
465-
if (literalSearch === true) {
466-
if (tmp === true) {
461+
if (literalSearch) {
462+
if (tmp) {
467463
return true;
468464
}
469465
continue;
@@ -474,7 +470,7 @@ window.initSearch = function(rawSearchIndex) {
474470
}
475471
}
476472
}
477-
return literalSearch === true ? false : lev_distance;
473+
return literalSearch ? false : lev_distance;
478474
}
479475

480476
function checkPath(contains, lastElem, ty) {
@@ -507,7 +503,7 @@ window.initSearch = function(rawSearchIndex) {
507503
}
508504
lev_total += lev;
509505
}
510-
if (aborted === false) {
506+
if (!aborted) {
511507
ret_lev = Math.min(ret_lev, Math.round(lev_total / clength));
512508
}
513509
}
@@ -634,14 +630,14 @@ window.initSearch = function(rawSearchIndex) {
634630
dontValidate: true,
635631
};
636632
}
637-
if (in_args === true && results_in_args[fullId] === undefined) {
633+
if (in_args && results_in_args[fullId] === undefined) {
638634
results_in_args[fullId] = {
639635
id: i,
640636
index: -1,
641637
dontValidate: true,
642638
};
643639
}
644-
if (returned === true && results_returned[fullId] === undefined) {
640+
if (returned && results_returned[fullId] === undefined) {
645641
results_returned[fullId] = {
646642
id: i,
647643
index: -1,
@@ -676,34 +672,34 @@ window.initSearch = function(rawSearchIndex) {
676672
fullId = ty.id;
677673

678674
returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
679-
if (output.name === "*" || returned === true) {
675+
if (output.name === "*" || returned) {
680676
in_args = false;
681677
var is_module = false;
682678

683679
if (input === "*") {
684680
is_module = true;
685681
} else {
686682
var allFound = true;
687-
for (it = 0, len = inputs.length; allFound === true && it < len; it++) {
683+
for (it = 0, len = inputs.length; allFound && it < len; it++) {
688684
allFound = checkType(type, inputs[it], true);
689685
}
690686
in_args = allFound;
691687
}
692-
if (in_args === true) {
688+
if (in_args) {
693689
results_in_args[fullId] = {
694690
id: i,
695691
index: -1,
696692
dontValidate: true,
697693
};
698694
}
699-
if (returned === true) {
695+
if (returned) {
700696
results_returned[fullId] = {
701697
id: i,
702698
index: -1,
703699
dontValidate: true,
704700
};
705701
}
706-
if (is_module === true) {
702+
if (is_module) {
707703
results[fullId] = {
708704
id: i,
709705
index: -1,
@@ -763,10 +759,10 @@ window.initSearch = function(rawSearchIndex) {
763759
}
764760
}
765761
if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
766-
if (typePassesFilter(typeFilter, ty.ty) === false) {
767-
lev = MAX_LEV_DISTANCE + 1;
768-
} else {
762+
if (typePassesFilter(typeFilter, ty.ty)) {
769763
lev += 1;
764+
} else {
765+
lev = MAX_LEV_DISTANCE + 1;
770766
}
771767
}
772768
in_args = findArg(ty, valGenerics, false, typeFilter);
@@ -821,7 +817,7 @@ window.initSearch = function(rawSearchIndex) {
821817
var ret = {
822818
"in_args": sortResults(results_in_args, true),
823819
"returned": sortResults(results_returned, true),
824-
"others": sortResults(results),
820+
"others": sortResults(results, false),
825821
};
826822
handleAliases(ret, query, filterCrates);
827823
return ret;
@@ -1263,7 +1259,7 @@ window.initSearch = function(rawSearchIndex) {
12631259
if (query.query.length === 0) {
12641260
return;
12651261
}
1266-
if (forced !== true && query.id === currentResults) {
1262+
if (!forced && query.id === currentResults) {
12671263
if (query.query.length > 0) {
12681264
searchState.putBackSearch(searchState.input);
12691265
}

src/librustdoc/html/static/source-script.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
4444
if (elem.dirs) {
4545
for (i = 0, len = elem.dirs.length; i < len; ++i) {
4646
if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
47-
hasFoundFile) === true) {
47+
hasFoundFile)) {
4848
addClass(name, "expand");
4949
hasFoundFile = true;
5050
}
@@ -59,8 +59,7 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
5959
var file = document.createElement("a");
6060
file.innerText = elem.files[i];
6161
file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
62-
if (hasFoundFile === false &&
63-
currentFile === fullPath + elem.files[i]) {
62+
if (!hasFoundFile && currentFile === fullPath + elem.files[i]) {
6463
file.className = "selected";
6564
addClass(name, "expand");
6665
hasFoundFile = true;
@@ -72,7 +71,7 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
7271
children.appendChild(files);
7372
parent.appendChild(name);
7473
parent.appendChild(children);
75-
return hasFoundFile === true && currentFile.startsWith(fullPath);
74+
return hasFoundFile && currentFile.startsWith(fullPath);
7675
}
7776

7877
function toggleSidebar() {
@@ -116,7 +115,7 @@ function createSidebarToggle() {
116115
// This function is called from "source-files.js", generated in `html/render/mod.rs`.
117116
// eslint-disable-next-line no-unused-vars
118117
function createSourceSidebar() {
119-
if (window.rootPath.endsWith("/") === false) {
118+
if (!window.rootPath.endsWith("/")) {
120119
window.rootPath += "/";
121120
}
122121
var main = document.getElementById("main");

0 commit comments

Comments
 (0)