unused_variable_detection.rs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. // SPDX-License-Identifier: Apache-2.0
  2. use solang::file_resolver::FileResolver;
  3. use solang::sema::ast;
  4. use solang::{parse_and_resolve, Target};
  5. use std::ffi::OsStr;
  6. fn parse(src: &'static str) -> ast::Namespace {
  7. let mut cache = FileResolver::new();
  8. cache.set_file_contents("test.sol", src.to_string());
  9. parse_and_resolve(OsStr::new("test.sol"), &mut cache, Target::EVM)
  10. }
  11. fn parse_two_files(src1: &'static str, src2: &'static str) -> ast::Namespace {
  12. let mut cache = FileResolver::new();
  13. cache.set_file_contents("test.sol", src1.to_string());
  14. cache.set_file_contents("test2.sol", src2.to_string());
  15. parse_and_resolve(OsStr::new("test.sol"), &mut cache, Target::EVM)
  16. }
  17. #[test]
  18. fn emit_event() {
  19. //Used event
  20. let case_1 = r#"
  21. contract usedEvent {
  22. event Hey(uint8 n);
  23. function emitEvent(uint8 n) public {
  24. emit Hey(n);
  25. }
  26. }
  27. "#;
  28. let ns = parse(case_1);
  29. assert_eq!(ns.diagnostics.count_warnings(), 0);
  30. // Unused event
  31. let case_2 = r#"
  32. event Hey(uint8 n);
  33. contract usedEvent {
  34. event Hey(uint8 n);
  35. event Hello(uint8 n);
  36. function emitEvent(uint8 n) public {
  37. emit Hey(n);
  38. }
  39. }
  40. "#;
  41. let ns = parse(case_2);
  42. assert_eq!(ns.diagnostics.count_warnings(), 1);
  43. assert_eq!(
  44. ns.diagnostics.first_warning().message,
  45. "event 'Hello' has never been emitted"
  46. );
  47. // Unused event
  48. let case_2 = r#"
  49. contract F {
  50. event Hey(uint8 n);
  51. event Hello(uint8 n);
  52. }
  53. contract usedEvent is F {
  54. event Hey(uint8 n);
  55. function emitEvent(uint8 n) public {
  56. emit Hey(n);
  57. }
  58. }
  59. "#;
  60. let ns = parse(case_2);
  61. assert_eq!(ns.diagnostics.count_warnings(), 1);
  62. assert_eq!(
  63. ns.diagnostics.first_warning().message,
  64. "event 'Hello' has never been emitted"
  65. );
  66. // Unused event
  67. let case_2 = r#"
  68. contract F {
  69. event Hey(uint8 n);
  70. }
  71. contract usedEvent is F {
  72. event Hey(uint8 n);
  73. function emitEvent(uint8 n) public {
  74. // reference event in contract F, so our event decl is not used
  75. emit F.Hey(n);
  76. }
  77. }
  78. "#;
  79. let ns = parse(case_2);
  80. assert_eq!(ns.diagnostics.count_warnings(), 1);
  81. assert_eq!(
  82. ns.diagnostics.first_warning().message,
  83. "event 'Hey' has never been emitted"
  84. );
  85. // make sure we don't complain about interfaces or abstract contracts
  86. let case_3 = r#"
  87. abstract contract F {
  88. event Hey(uint8 n);
  89. }
  90. interface G {
  91. event Hey(uint8 n);
  92. }
  93. "#;
  94. let ns = parse(case_3);
  95. assert_eq!(ns.diagnostics.count_warnings(), 0);
  96. }
  97. #[test]
  98. fn constant_variable() {
  99. let file_2 = r#"
  100. uint32 constant outside = 2;
  101. "#;
  102. let file_1 = r#"
  103. import "test2.sol";
  104. contract Testing {
  105. uint32 test;
  106. uint32 constant cte = 5;
  107. constructor() {
  108. test = outside;
  109. test = cte;
  110. }
  111. function get() public view returns (uint32) {
  112. return test;
  113. }
  114. }
  115. "#;
  116. //Constant properly read
  117. let ns = parse_two_files(file_1, file_2);
  118. assert_eq!(ns.diagnostics.count_warnings(), 0);
  119. let file_1 = r#"
  120. import "test2.sol";
  121. contract Testing {
  122. uint32 test;
  123. uint32 constant cte = 5;
  124. constructor() {
  125. test = 45;
  126. }
  127. function get() public view returns (uint32) {
  128. return test;
  129. }
  130. }
  131. "#;
  132. let ns = parse_two_files(file_1, file_2);
  133. assert_eq!(ns.diagnostics.count_warnings(), 2);
  134. assert!(ns
  135. .diagnostics
  136. .warning_contains("storage variable 'cte' has been assigned, but never read"));
  137. assert!(ns
  138. .diagnostics
  139. .warning_contains("global constant 'outside' has never been used"));
  140. }
  141. #[test]
  142. fn storage_variable() {
  143. let file = r#"
  144. contract Test {
  145. string str = "This is a test";
  146. string str2;
  147. constructor() {
  148. str = "This is another test";
  149. }
  150. }
  151. "#;
  152. let ns = parse(file);
  153. let warnings = ns.diagnostics.warnings();
  154. assert_eq!(warnings.len(), 2);
  155. assert_eq!(
  156. warnings[0].message,
  157. "storage variable 'str' has been assigned, but never read"
  158. );
  159. assert_eq!(
  160. warnings[1].message,
  161. "storage variable 'str2' has never been used"
  162. );
  163. let file = r#"
  164. contract Test {
  165. string str = "This is a test";
  166. string str2;
  167. constructor() {
  168. str = "This is another test";
  169. str2 = str;
  170. }
  171. }
  172. "#;
  173. let ns = parse(file);
  174. assert_eq!(ns.diagnostics.count_warnings(), 1);
  175. assert_eq!(
  176. ns.diagnostics.first_warning().message,
  177. "storage variable 'str2' has been assigned, but never read"
  178. );
  179. let file = r#"
  180. contract Test {
  181. string str = "This is a test";
  182. constructor() {
  183. str = "This is another test";
  184. }
  185. }
  186. contract Test2 is Test {
  187. function get() public view returns (string) {
  188. return str;
  189. }
  190. }
  191. "#;
  192. let ns = parse(file);
  193. assert_eq!(ns.diagnostics.count_warnings(), 0);
  194. }
  195. #[test]
  196. fn state_variable() {
  197. let file = r#"
  198. contract Test {
  199. function get() public pure {
  200. uint32 a = 1;
  201. uint32 b;
  202. b = 1;
  203. uint32 c;
  204. uint32 d;
  205. d = 1;
  206. uint32 e;
  207. e = d*5;
  208. d = e/5;
  209. }
  210. }
  211. "#;
  212. let ns = parse(file);
  213. assert_eq!(ns.diagnostics.count_warnings(), 3);
  214. assert!(ns
  215. .diagnostics
  216. .warning_contains("local variable 'b' has been assigned, but never read"));
  217. assert!(ns
  218. .diagnostics
  219. .warning_contains("local variable 'a' has been assigned, but never read"));
  220. assert!(ns
  221. .diagnostics
  222. .warning_contains("local variable 'c' has never been read nor assigned"));
  223. }
  224. #[test]
  225. fn struct_usage() {
  226. let file = r#"
  227. struct testing {
  228. uint8 it;
  229. bool tf;
  230. }
  231. contract Test {
  232. testing t1;
  233. testing t4;
  234. testing t6;
  235. constructor() {
  236. t1 = testing(8, false);
  237. }
  238. function modify() public returns (uint8) {
  239. testing memory t2;
  240. t2.it = 4;
  241. t4 = testing(1, true);
  242. testing storage t3 = t4;
  243. uint8 k = 2*4/t3.it;
  244. testing t5;
  245. return k;
  246. }
  247. }
  248. "#;
  249. let ns = parse(file);
  250. assert_eq!(ns.diagnostics.count_warnings(), 4);
  251. assert!(ns
  252. .diagnostics
  253. .warning_contains("local variable 't2' has been assigned, but never read"));
  254. assert!(ns
  255. .diagnostics
  256. .warning_contains("storage variable 't1' has been assigned, but never read"));
  257. assert!(ns
  258. .diagnostics
  259. .warning_contains("local variable 't5' has never been read nor assigned"));
  260. assert!(ns
  261. .diagnostics
  262. .warning_contains("storage variable 't6' has never been used"));
  263. }
  264. #[test]
  265. fn subscript() {
  266. let file = r#"
  267. contract Test {
  268. int[] arr1;
  269. int[4] arr2;
  270. int[4] arr3;
  271. bytes byteArr;
  272. uint constant e = 1;
  273. function get() public {
  274. uint8 a = 1;
  275. uint8 b = 2;
  276. arr1[a] = 1;
  277. arr2[a+b] = 2;
  278. uint8 c = 1;
  279. uint8 d = 1;
  280. int[] memory arr4;
  281. arr4[0] = 1;
  282. int[4] storage arr5 = arr3;
  283. arr5[c*d] = 1;
  284. byteArr[e] = 0x05;
  285. }
  286. }
  287. "#;
  288. let ns = parse(file);
  289. assert_eq!(ns.diagnostics.count_warnings(), 4);
  290. assert!(ns
  291. .diagnostics
  292. .warning_contains("local variable 'arr4' has been assigned, but never read"));
  293. assert!(ns
  294. .diagnostics
  295. .warning_contains("storage variable 'arr1' has been assigned, but never read"));
  296. assert!(ns
  297. .diagnostics
  298. .warning_contains("storage variable 'arr2' has been assigned, but never read"));
  299. assert!(ns
  300. .diagnostics
  301. .warning_contains("storage variable 'byteArr' has been assigned, but never read"));
  302. let file = r#"
  303. contract Test {
  304. int[] arr1;
  305. int[4] arr2;
  306. int[4] arr3;
  307. bytes byteArr;
  308. uint constant e = 1;
  309. function get() public {
  310. uint8 a = 1;
  311. uint8 b = 2;
  312. arr1[a] = 1;
  313. arr2[a+b] = 2;
  314. assert(arr1[a] == arr2[b]);
  315. uint8 c = 1;
  316. uint8 d = 1;
  317. int[] memory arr4;
  318. arr4[0] = 1;
  319. int[4] storage arr5 = arr3;
  320. arr5[c*d] = 1;
  321. assert(arr4[c] == arr5[d]);
  322. assert(arr3[c] == arr5[d]);
  323. byteArr[e] = 0x05;
  324. assert(byteArr[e] == byteArr[e]);
  325. }
  326. }
  327. "#;
  328. let ns = parse(file);
  329. assert_eq!(ns.diagnostics.count_warnings(), 0);
  330. }
  331. #[test]
  332. fn assign_trunc_cast() {
  333. //This covers ZeroExt as well
  334. let file = r#"
  335. contract Test {
  336. bytes byteArr;
  337. bytes32 baRR;
  338. function get() public {
  339. string memory s = "Test";
  340. byteArr = bytes(s);
  341. uint16 a = 1;
  342. uint8 b;
  343. b = uint8(a);
  344. uint256 c;
  345. c = b;
  346. bytes32 b32;
  347. bytes memory char = bytes(bytes32(uint(a) * 2 ** (8 * b)));
  348. baRR = bytes32(c);
  349. bytes32 cdr = bytes32(char);
  350. assert(b32 == baRR);
  351. if(b32 != cdr) {
  352. }
  353. }
  354. }
  355. "#;
  356. let ns = parse(file);
  357. assert_eq!(ns.diagnostics.count_warnings(), 1);
  358. assert!(ns
  359. .diagnostics
  360. .warning_contains("storage variable 'byteArr' has been assigned, but never read"));
  361. }
  362. #[test]
  363. fn array_length() {
  364. let file = r#"
  365. contract Test {
  366. int[5] arr1;
  367. int[] arr2;
  368. function get() public view returns (bool) {
  369. int[5] memory arr3;
  370. int[] memory arr4;
  371. bool test = false;
  372. if(arr1.length == arr2.length) {
  373. test = true;
  374. }
  375. else if(arr3.length != arr4.length) {
  376. test = false;
  377. }
  378. return test;
  379. }
  380. }
  381. "#;
  382. let ns = parse(file);
  383. assert_eq!(ns.diagnostics.count_warnings(), 0);
  384. }
  385. #[test]
  386. fn sign_ext_storage_load() {
  387. let file = r#"
  388. contract Test {
  389. bytes a;
  390. function use(bytes memory b) pure public {
  391. assert(b[0] == b[1]);
  392. }
  393. function get() public pure returns (int16 ret) {
  394. use(a);
  395. int8 b = 1;
  396. int16 c = 1;
  397. int16 d;
  398. d = c << b;
  399. ret = d;
  400. }
  401. }
  402. "#;
  403. let ns = parse(file);
  404. assert_eq!(ns.diagnostics.count_warnings(), 0);
  405. }
  406. #[test]
  407. fn statements() {
  408. let file = r#"
  409. contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
  410. contract Example {
  411. AddNumbers addContract;
  412. event StringFailure(string stringFailure);
  413. event BytesFailure(bytes bytesFailure);
  414. function exampleFunction(uint256 _a, uint256 _b) public returns (uint256 _c) {
  415. try addContract.add(_a, _b) returns (uint256 _value) {
  416. return (_value);
  417. } catch Error(string memory _err) {
  418. // This may occur if there is an overflow with the two numbers and the `AddNumbers` contract explicitly fails with a `revert()`
  419. emit StringFailure(_err);
  420. } catch (bytes memory _err) {
  421. emit BytesFailure(_err);
  422. }
  423. }
  424. function testFunction() pure public {
  425. int three = 3;
  426. {
  427. int test = 2;
  428. int c = test*3;
  429. while(c != test) {
  430. c -= three;
  431. }
  432. }
  433. int four = 4;
  434. int test = 3;
  435. do {
  436. int ct = 2;
  437. } while(four > test);
  438. }
  439. function bytesToUInt(uint v) public pure returns (uint ret) {
  440. if (v == 0) {
  441. ret = 0;
  442. }
  443. else {
  444. while (v > 0) {
  445. ret = uint(uint(ret) / (2 ** 8));
  446. ret |= uint(((v % 10) + 48) * 2 ** (8 * 31));
  447. v /= 10;
  448. }
  449. }
  450. return ret;
  451. }
  452. function stringToUint(string s) public pure returns (uint result) {
  453. bytes memory b = bytes(s);
  454. uint i;
  455. result = 0;
  456. for (i = 0; i < b.length; i++) {
  457. uint c = uint(b[i]);
  458. if (c >= 48 && c <= 57) {
  459. result = result * 10 + (c - 48);
  460. }
  461. }
  462. }
  463. }
  464. "#;
  465. let ns = parse(file);
  466. assert_eq!(ns.diagnostics.count_warnings(), 2);
  467. assert!(ns
  468. .diagnostics
  469. .warning_contains("function parameter 'a' has never been read"));
  470. assert!(ns
  471. .diagnostics
  472. .warning_contains("local variable 'ct' has been assigned, but never read",));
  473. }
  474. #[test]
  475. fn function_call() {
  476. let file = r#"
  477. contract Test1 {
  478. uint32 public a;
  479. constructor(uint32 b) {
  480. a = b;
  481. }
  482. }
  483. contract Test2{
  484. function test(uint32 v1, uint32 v2) private returns (uint32) {
  485. uint32 v = 1;
  486. Test1 t = new Test1(v);
  487. uint32[2] memory vec = [v2, v1];
  488. return vec[0] + t.a();
  489. }
  490. function callTest() public {
  491. uint32 ta = 1;
  492. uint32 tb = 2;
  493. ta = test(ta, tb);
  494. }
  495. }
  496. contract C {
  497. uint public data;
  498. function x() public returns (uint) {
  499. data = 3;
  500. return this.data();
  501. }
  502. }
  503. "#;
  504. let ns = parse(file);
  505. assert_eq!(ns.diagnostics.count_warnings(), 0);
  506. let file = r#"
  507. contract Test1 {
  508. uint32 public a;
  509. constructor(uint32 b) {
  510. a = b;
  511. }
  512. }
  513. contract Test2 is Test1{
  514. constructor(uint32 val) Test1(val) {}
  515. function test(uint32 v1, uint32 v2) private returns (uint32) {
  516. uint32 v = 1;
  517. Test1 t = new Test1(v);
  518. uint32[2] memory vec = [v2, v1];
  519. return vec[0] + t.a();
  520. }
  521. function callTest() public {
  522. uint32 ta = 1;
  523. uint32 tb = 2;
  524. ta = test(ta, tb);
  525. }
  526. }
  527. "#;
  528. let ns = parse(file);
  529. assert_eq!(ns.diagnostics.count_warnings(), 0);
  530. }
  531. #[test]
  532. fn array_push_pop() {
  533. let file = r#"
  534. contract Test1 {
  535. uint32[] vec1;
  536. function testVec() public {
  537. uint32 a = 1;
  538. uint32 b = 2;
  539. uint32[] memory vec2;
  540. vec1.push(a);
  541. vec2.push(b);
  542. }
  543. }
  544. "#;
  545. let ns = parse(file);
  546. assert_eq!(ns.diagnostics.count_warnings(), 2);
  547. assert!(ns
  548. .diagnostics
  549. .warning_contains("local variable 'vec2' has been assigned, but never read"));
  550. assert!(ns
  551. .diagnostics
  552. .warning_contains("storage variable 'vec1' has been assigned, but never read"));
  553. let file = r#"
  554. contract Test1 {
  555. uint32[] vec1;
  556. function testVec() public {
  557. uint32 a = 1;
  558. uint32 b = 2;
  559. uint32[] memory vec2;
  560. vec1.push(a);
  561. vec2.push(b);
  562. vec1.pop();
  563. vec2.pop();
  564. }
  565. }
  566. "#;
  567. let ns = parse(file);
  568. assert_eq!(ns.diagnostics.count_warnings(), 0);
  569. }
  570. #[test]
  571. fn return_variable() {
  572. let file = r#"
  573. contract Test1 {
  574. string testing;
  575. function test1() public pure returns (uint32 ret, string memory ret2) {
  576. return (2, "Testing is fun");
  577. }
  578. function test2() public returns (uint32 hey) {
  579. (uint32 a, string memory t) = test1();
  580. testing = t;
  581. }
  582. }
  583. "#;
  584. let ns = parse(file);
  585. assert_eq!(ns.diagnostics.count_warnings(), 3);
  586. assert!(ns
  587. .diagnostics
  588. .warning_contains("destructure variable 'a' has never been used"));
  589. assert!(ns
  590. .diagnostics
  591. .warning_contains("return variable 'hey' has never been assigned"));
  592. assert!(ns
  593. .diagnostics
  594. .warning_contains("storage variable 'testing' has been assigned, but never read"));
  595. }
  596. #[test]
  597. fn try_catch() {
  598. let file = r#"
  599. contract CalledContract {}
  600. contract TryCatcher {
  601. event SuccessEvent(bool t);
  602. event CatchEvent(bool t);
  603. function execute() public {
  604. try new CalledContract() returns(CalledContract returnedInstance) {
  605. emit SuccessEvent(true);
  606. } catch Error(string memory revertReason) {
  607. emit CatchEvent(true);
  608. } catch (bytes memory returnData) {
  609. emit CatchEvent(false);
  610. }
  611. }
  612. }
  613. "#;
  614. let ns = parse(file);
  615. assert_eq!(ns.diagnostics.count_warnings(), 3);
  616. assert!(ns
  617. .diagnostics
  618. .warning_contains("try-catch error bytes 'returnData' has never been used"));
  619. assert!(ns
  620. .diagnostics
  621. .warning_contains("try-catch returns variable 'returnedInstance' has never been read"));
  622. assert!(ns
  623. .diagnostics
  624. .warning_contains("try-catch error string 'revertReason' has never been used"));
  625. let file = r#"
  626. contract CalledContract {
  627. bool public ok = true;
  628. bool private notOk = false;
  629. }
  630. contract TryCatcher {
  631. event SuccessEvent(bool t);
  632. event CatchEvent(string t);
  633. event CatchBytes(bytes t);
  634. function execute() public {
  635. try new CalledContract() returns(CalledContract returnedInstance) {
  636. // returnedInstance can be used to obtain the address of the newly deployed contract
  637. emit SuccessEvent(returnedInstance.ok());
  638. } catch Error(string memory revertReason) {
  639. emit CatchEvent(revertReason);
  640. } catch (bytes memory returnData) {
  641. emit CatchBytes(returnData);
  642. }
  643. }
  644. }
  645. "#;
  646. let ns = parse(file);
  647. assert_eq!(ns.diagnostics.count_warnings(), 1);
  648. assert!(ns
  649. .diagnostics
  650. .warning_contains("storage variable 'notOk' has been assigned, but never read"));
  651. let file = r#"
  652. contract CalledContract {
  653. bool public ok;
  654. }
  655. "#;
  656. let ns = parse(file);
  657. assert_eq!(ns.diagnostics.count_warnings(), 0);
  658. }
  659. #[test]
  660. fn destructure() {
  661. let file = r#"
  662. contract Test2{
  663. function callTest() public view returns (uint32 ret) {
  664. uint32 ta = 1;
  665. uint32 tb = 2;
  666. uint32 te = 3;
  667. string memory tc = "hey";
  668. bytes memory td = bytes(tc);
  669. address nameReg = address(this);
  670. (bool tf,) = nameReg.call(td);
  671. ta = tf? tb : te;
  672. uint8 tg = 1;
  673. uint8 th = 2;
  674. (tg, th) = (th, tg);
  675. return ta;
  676. }
  677. }
  678. "#;
  679. let ns = parse(file);
  680. assert_eq!(ns.diagnostics.count_warnings(), 0);
  681. }
  682. #[test]
  683. fn struct_initialization() {
  684. let file = r#"
  685. contract Test1{
  686. struct Test2{
  687. uint8 a;
  688. uint8 b;
  689. }
  690. function callTest() public pure returns (uint32 ret) {
  691. uint8 tg = 1;
  692. uint8 th = 2;
  693. Test2 memory t2;
  694. t2 = Test2(tg, th);
  695. ret = t2.a;
  696. }
  697. }
  698. "#;
  699. let ns = parse(file);
  700. assert_eq!(ns.diagnostics.count_warnings(), 0);
  701. }
  702. #[test]
  703. fn subarray_mapping_struct_literal() {
  704. let file = r#"
  705. contract T {
  706. int p;
  707. constructor(int b) {
  708. p = b;
  709. }
  710. function sum(int a, int b) virtual public returns (int){
  711. uint8 v1 = 1;
  712. uint8 v2 = 2;
  713. uint8 v3 = 3;
  714. uint8 v4 = 4;
  715. uint8[2][2] memory v = [[v1, v2], [v3, v4]];
  716. return a + b * p/v[0][1];
  717. }
  718. }
  719. contract Test is T(2){
  720. struct fooStruct {
  721. int foo;
  722. int figther;
  723. }
  724. mapping(string => int) public mp;
  725. enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
  726. FreshJuiceSize choice;
  727. function sum(int a, int b) override public returns (int) {
  728. choice = FreshJuiceSize.LARGE;
  729. return a*b;
  730. }
  731. function test() public returns (int){
  732. int a = 1;
  733. int b = 2;
  734. int c = super.sum(a, b);
  735. int d = 3;
  736. fooStruct memory myStruct = fooStruct({foo: c, figther: d});
  737. string memory t = "Do some tests";
  738. mp[t] = myStruct.figther;
  739. return mp[t];
  740. }
  741. }
  742. "#;
  743. let ns = parse(file);
  744. assert_eq!(ns.diagnostics.count_warnings(), 1);
  745. assert!(ns
  746. .diagnostics
  747. .warning_contains("storage variable 'choice' has been assigned, but never read"));
  748. }
  749. #[test]
  750. fn builtin_call_destructure() {
  751. let file = r#"
  752. contract Test {
  753. function test() public returns(bool p) {
  754. uint128 b = 1;
  755. uint64 g = 2;
  756. address payable ad = payable(address(this));
  757. bytes memory by = hex"AB2";
  758. (p, ) = ad.call{value: b, gas: g}(by);
  759. uint c = 1;
  760. abi.encodeWithSignature("hey", c);
  761. uint128 amount = 2;
  762. ad.send(amount);
  763. uint128 amount2 = 1;
  764. ad.transfer(amount2);
  765. }
  766. }
  767. "#;
  768. let ns = parse(file);
  769. assert_eq!(ns.diagnostics.count_warnings(), 0);
  770. }
  771. #[test]
  772. fn delete_statement() {
  773. let file = r#"
  774. pragma solidity 0;
  775. contract Test1{
  776. int test8var;
  777. function test8() public {
  778. delete test8var;
  779. test8var = 2;
  780. }
  781. }
  782. "#;
  783. let ns = parse(file);
  784. assert_eq!(ns.diagnostics.count_warnings(), 0);
  785. }
  786. #[test]
  787. fn load_length() {
  788. let file = r#"
  789. contract foo {
  790. function f(uint i1) public pure returns (int) {
  791. int[8] bar = [ int(10), 20, 30, 4, 5, 6, 7, 8 ];
  792. bar[2] = 0x7_f;
  793. return bar[i1];
  794. }
  795. function barfunc() public pure returns (uint) {
  796. uint[2][3][4] array;
  797. return array.length;
  798. }
  799. }
  800. "#;
  801. let ns = parse(file);
  802. assert_eq!(ns.diagnostics.count_warnings(), 0);
  803. }
  804. #[test]
  805. fn address_selector() {
  806. let file = r#"
  807. contract ctc {
  808. function foo(int32 a) public pure returns (bool) {
  809. return a==1;
  810. }
  811. function test() public view {
  812. function(int32) external returns (bool) func = this.foo;
  813. assert(address(this) == func.address);
  814. assert(func.selector == hex"42761137");
  815. }
  816. }
  817. "#;
  818. let ns = parse(file);
  819. assert_eq!(ns.diagnostics.count_warnings(), 0);
  820. }
  821. #[test]
  822. fn load_storage_load() {
  823. let file = r#"
  824. struct X {
  825. uint32 f1;
  826. bool f2;
  827. }
  828. contract foo {
  829. function get() public pure returns (X[4] f) {
  830. f[1].f1 = 102;
  831. f[1].f2 = true;
  832. }
  833. }
  834. "#;
  835. let ns = parse(file);
  836. assert_eq!(ns.diagnostics.count_warnings(), 0);
  837. }
  838. #[test]
  839. fn variable_function() {
  840. let file = r#"
  841. contract ft is Arith {
  842. function test(bool action, int32 a, int32 b) public returns (int32) {
  843. function(int32,int32) internal returns (int32) func;
  844. if (action) {
  845. func = Arith.mul;
  846. } else {
  847. func = Arith.add;
  848. }
  849. return func(a, b);
  850. }
  851. }
  852. contract Arith {
  853. function mul(int32 a, int32 b) internal pure returns (int32) {
  854. return a * b;
  855. }
  856. function add(int32 a, int32 b) internal pure returns (int32) {
  857. return a + b;
  858. }
  859. }
  860. "#;
  861. let ns = parse(file);
  862. assert_eq!(ns.diagnostics.count_warnings(), 0);
  863. let file = r#"
  864. contract ft {
  865. function test() public {
  866. function(int32) external returns (uint64) func = this.foo;
  867. assert(func(102) == 0xabbaabba);
  868. }
  869. function foo(int32) public pure returns (uint64) {
  870. return 0xabbaabba;
  871. }
  872. }
  873. "#;
  874. let ns = parse(file);
  875. assert_eq!(ns.diagnostics.count_warnings(), 0);
  876. let file = r#"
  877. contract ft {
  878. function(int32,int32) internal returns (int32) func;
  879. function mul(int32 a, int32 b) internal pure returns (int32) {
  880. return a * b;
  881. }
  882. function add(int32 a, int32 b) internal pure returns (int32) {
  883. return a + b;
  884. }
  885. function set_op(bool action) public {
  886. if (action) {
  887. func = mul;
  888. } else {
  889. func = add;
  890. }
  891. }
  892. function test(int32 a, int32 b) public returns (int32) {
  893. return func(a, b);
  894. }
  895. }
  896. "#;
  897. let ns = parse(file);
  898. assert_eq!(ns.diagnostics.count_warnings(), 0);
  899. let file = r#"
  900. contract ft {
  901. function mul(int32 a, int32 b) internal pure returns (int32) {
  902. return a * b;
  903. }
  904. function add(int32 a, int32 b) internal pure returns (int32) {
  905. return a + b;
  906. }
  907. function test(bool action, int32 a, int32 b) public returns (int32) {
  908. function(int32,int32) internal returns (int32) func;
  909. if (action) {
  910. func = mul;
  911. } else {
  912. func = add;
  913. }
  914. return func(a, b);
  915. }
  916. }
  917. "#;
  918. let ns = parse(file);
  919. assert_eq!(ns.diagnostics.count_warnings(), 0);
  920. let file = r#"
  921. contract ft is Arith {
  922. function mul(int32 a, int32 b) internal pure override returns (int32) {
  923. return a * b * 10;
  924. }
  925. function add(int32 a, int32 b) internal pure override returns (int32) {
  926. return a + b + 10;
  927. }
  928. }
  929. contract Arith {
  930. function test(bool action, int32 a, int32 b) public returns (int32) {
  931. function(int32,int32) internal returns (int32) func;
  932. if (action) {
  933. func = mul;
  934. } else {
  935. func = add;
  936. }
  937. return func(a, b);
  938. }
  939. function mul(int32 a, int32 b) internal virtual returns (int32) {
  940. return a * b;
  941. }
  942. function add(int32 a, int32 b) internal virtual returns (int32) {
  943. return a + b;
  944. }
  945. }
  946. "#;
  947. let ns = parse(file);
  948. assert_eq!(ns.diagnostics.count_warnings(), 0);
  949. let file = r#"
  950. function global_function() pure returns (uint32) {
  951. return 102;
  952. }
  953. function global_function2() pure returns (uint32) {
  954. return global_function() + 5;
  955. }
  956. contract c {
  957. function test() public {
  958. function() internal returns (uint32) ftype = global_function2;
  959. uint64 x = ftype();
  960. assert(x == 107);
  961. }
  962. }
  963. "#;
  964. let ns = parse(file);
  965. assert_eq!(ns.diagnostics.count_warnings(), 0);
  966. }
  967. #[test]
  968. fn format_string() {
  969. let file = r#"
  970. contract foo {
  971. constructor() {
  972. int x = 21847450052839212624230656502990235142567050104912751880812823948662932355201;
  973. print("x = {}".format(x));
  974. }
  975. }
  976. "#;
  977. let ns = parse(file);
  978. assert_eq!(ns.diagnostics.count_warnings(), 0);
  979. }
  980. #[test]
  981. fn balance() {
  982. let file = r#"
  983. contract foo {
  984. function test(address payable addr) public pure returns (bool) {
  985. bool p;
  986. p = addr.balance == 2;
  987. return p;
  988. }
  989. }
  990. "#;
  991. let ns = parse(file);
  992. assert_eq!(ns.diagnostics.count_warnings(), 0);
  993. }